MVC Complaint: Checkboxes

December 11, 2007 10:42 AM

NOTE:
This article was written for the December CTP release of the MVC framework. Unfortunately, it does not entirely apply to the Preview 2 release or subsequent releases.

I mentioned in my previous post on building login & registration forms in the ASP.Net MVC framework that I was not a fan of the way checkboxes are handled in the first CTP release's toolkit. To help explain why I don't care for it, consider the following... what would you expect the following helper function to output?

<% =Html.CheckBox( "rememberMe", "Remember Me", true ) %>

 

I would imagine a single checkbox and a label with the text value, which is incorrect. Instead, this is what is generated:

<input type="checkbox" id="rememberMe" name="rememberMe" value="Remember Me" checked=checked />Remember Me

 

It looks fairly reasonable to most and really isn't the end of the world, but is rather unusable in my eyes. The text is just output inline directly following the checkbox element with no element wrapping it. This small omission makes it incredibly difficult to style the output effectively using CSS. Simply wrap the text in a <label> element and everything is gravy. What I would prefer to see output is:

<input type="checkbox" id="rememberMe" name="rememberMe"  value="Remember Me" checked="checked" />

<label for="rememberMe">Remember Me</label>

 

I realize this seems like a nitpick; I'm perfectly capable of writing my own HTML checkbox and label (usually). My frustrations do not end there, though; let's look at how you retrieve data from a checkbox within a controller action that you have posted to. Taking the example of a simple login form with two text fields ("userName" and "password") and one checkbox ("rememberMe") you might expect to have the following controller action:

[ControllerAction]

public void Authenticate( string userName, string password, bool rememberMe )

{

    if( rememberMe ){ ... }

}

 

Given the context of this article, you can probably guess that the above doesn't work. Unfortunately the automagic code that maps fields found in the HTTP request to parameters in a controller action does not support mapping to a boolean. This is partly the fault of the HTTP spec (submitting a checked checkbox causes the form to post with $NAME=$VALUE where $NAME is the name of the checkbox element and $VALUE is the value of the "value" attribute; submitting an unchecked checkbox causes the form to post without any reference to the checkbox -- i.e.: a null value), but could easily be resolved with the following rule:

When a boolean is declared in a method marked with [ControllerAction], pass false into the parameter unless a text value is found with a field name that correlates to the name of the parameter (in which case pass true).

In other words, if you can't find a mention of "rememberMe" in the form's POST data, pass false to the parameter. If the form's POST data contains any value for a field named "rememberMe," pass true to the parameter. For the time being you should declare the checkbox parameter as a string and test it against null, like so:

[ControllerAction]

public void Authenticate( string userName, string password, string rememberMe )

{

    if( rememberMe != null ){ ... }

}

 

Sure, it isn't rocket science the way it is setup right now, but a little extra work could make for a much more fluid learning curve for new MVC developers. Obviously this is just a CTP release and not the full deal; I fully expect a lot of these quirks to be worked out over the next several months. This does make me wonder, will the toolkit be posted to CodePlex so that it can benefit from community involvement?

Tags: , ,
Categories: MVC
Actions: E-mail | Permalink | Comments (10) RSS Feed for this post's comments.

Comments

12/11/2007 10:45 AM #

trackback


Trackback from: DotNetKicks.com

12/11/2007 11:24 AM #

Danny

Good Point. I don't think this is being overly particular. Hopefully these changes can make it into the release!

Danny us

12/11/2007 2:45 PM #

Andrey Shchekin

Does nullable bool work correctly (true or null)?

Andrey Shchekin ru

12/11/2007 4:04 PM #

trackback


Trackback from: Danny Douglass

12/11/2007 4:58 PM #

Troy Goode


Hi Andrey,

Unfortunately no, using a parameter named "bool? rememberMe" does not work. If you try to do that "rememberMe" will always be null, regardless of whether the box was checked or not.

Troy Goode us

1/13/2008 8:33 PM #

Ryan Cromwell

You can, in fact, make the checkbox work correctly using both the bool?/Nullable<bool> parameter and setting the value of the checkbox/input as True.

Here's more: cromwellhaus.com/.../...boolean-value-mapping.aspx

Ryan Cromwell us

1/13/2008 9:58 PM #

Troy Goode

Thanks Ryan, that is a great tip. I had tried the bool? parameter before, but had not tried setting the checkbox's value to "true", which does make sense in hindsight.

Troy Goode us

3/11/2008 3:40 PM #

zaina

hi i need ur help about the MVC for java.swing component

zaina jo

7/3/2008 8:36 AM #

Ryan Cromwell

Troy - have you been able to get this to work with Preview 3?

Ryan Cromwell us

7/3/2008 12:46 PM #

Troy Goode

@Ryan:
I just double-checked and yes, everything still works in Preview 3. What issue are you encountering?

Troy Goode us

Add Comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading




Troy Goode

Troy Goode
Microsoft Certified Professional Developer
AddThis Feed Button

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in  anyway.

© Copyright 2008

Colophon

Powered by:
BlogEngine.NET 1.4
Template:
Designs by Darren
Header Font:
Stamper
Syntax Highlighting:
WLW Code Snippet Plugin