Recently in ASP.NET MVC Category

I've been using ASP.NET MVC since preview 1, and have loved every bit of it.  Between preview releases, none of my code broke -- even between 2 and 3...  Wait that can't be right...

So when the MVC beta dropped recently, I went ahead and upgraded my three projects.  Little did I know that when I was upgrading all of those other times, I was forgetting to update my assembly references!  Big d'oh!  I had been using preview 2 this whole time!

Upgrading from preview 2 to the beta can be a bit of a pain, so here are some hints to make the process easier, if anyone out there actually needs to do this.

  • Update your strongly typed ViewData references to use ViewData.Model - This one is pretty self-explanitory.  If you have strongly typed ViewData (by typing ViewData<Foo> in your codebehind) you need to reference ViewData.Model in your views now.
  • ViewData is now ViewDataDictionary - If you have custom ViewData-derived classes, you should change them to inherit ViewDataDictionary.  I just got rid of mine completely and created some ViewInfo classes to pack up all the common data I needed to send over that wasn't my model object.
  • RenderView("Page") is now return View("Page") - Much, much nicer.  Make sure you change those public voids to public ActionResults as well.  Good work on this change!
  • Default.aspx has a codebehind page now - If you're getting blank pages, you probably want to update your Default.aspx that is included in the project template.  Just create a new MVC beta project and you should have it ready to copy into your old project.
  • Update your Web.config file - There are a few new entries:
In compilation/assemblies: <add assembly="System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
In pages/namespaces: <add namespace="System.Web.Mvc.Html" />

Happy coding!

I've been working on an ASP.NET MVC project with a nice and fancy model layer. My models have a nice'n'friendly UpdateFrom(NameValueCollection) method to bind HTTP request parameters to an object. The only problem is that sometimes, I don't want to have every column on a form, and some of the related database columns should remain unchanged.

In HTML forms, this presents a problem: checkboxes that are unchecked don't post anything. How am I to tell if a field has been left off the form or if a field is unchecked? After some aimless wanderings on the inter-tubes, I found this article which documents a solution for Rails.

It's pretty simple, so I figured I would take this idea and wrap it up in a server control. We have a small set of server WebControls that play nice with the rest of our code, so it was just a matter of adding a few lines of code. You could also wrap this up in a helper method like in the rails version.

CheckBox checkbox = new CheckBox();
checkbox.InputAttributes.Add("class", CssClass);
checkbox.InputAttributes.Add("id", (string.IsNullOrEmpty(ID) ? (FieldName + "CheckBox") : ID));
checkbox.InputAttributes.Add("value", "true");
checkbox.InputAttributes.Add("onclick", 
    "document.getElementById(\"" + FieldName + "TextBox\").value = this.checked.toString()");
checkbox.Checked = Checked;
checkbox.RenderControl(writer);

TextBox textbox = new TextBox();
textbox.Attributes.Add("style", "display: none;");
textbox.Attributes.Add("id", FieldName + "TextBox");
textbox.Attributes.Add("name", FieldName);
textbox.Text = Checked ? "true" : "false";
textbox.RenderControl(writer);

Now I can tell if a checkbox field is truly meant to either just not be there or is false.

Powered by Movable Type 4.21-en

Recent Comments

  • Leandro Nuñez: Hey, thanks for the great share. Is there a way read more
  • Justin: cool plugin. how would you go about creating then wiring read more
  • Anil: Nice plugin. I wanted to add some validation and stop read more
  • reigel: look cool... It would be okay if it can validate read more
  • glompix: @derby We actually do use Subverison for our version control, read more
  • derby: I use xperl, dominos, and titan bar to customize my read more
  • derby: Thanks for the script. I was looking for some AJAX read more
  • A. burton: how do you set up a search for events created read more
  • NANERPUSS: LEXINGTON GOONTUCKY :hfive: read more
  • Matt: This is a great walkthrough. I am also using the read more