Mapping a Checkbox To a Boolean Action Parameter in ASP.NET MVC

An entry about asp.net mvc Publication date 23. August 2008 13:38

Say you have some controller action that takes a boolean:

public ActionResult Login(string username, string password, bool rememberMe)
{
    // authenticate...
}

Now, you want to let the user set the “Remember me?” option using a check box:

<input type="checkbox" id="rememberMe" name="rememberMe" />
<label
for="rememberMe">Remember me?</label>

This won’t work! A checkbox is an input element, so when it gets posted, it posts its value - and a checkbox has the value ‘on’ by default (which cannot be parses as a boolean). You might think that we could use a bit of JavaScript trickery to change the value depending on the checked/unchecked state:

<input type="checkbox" id="rememberMe" name="rememberMe" 
onchange="this.value = this.checked ? 'true' : 'false'" />
<label
for="rememberMe">Remember me?</label>

This won’t work either! The semantics of a checkbox are a little bit different from what you may think; instead of posting a value indicating its checked/unchecked state, a checked checkbox posts whatever is in its ‘value’ attribute, and an unchecked checkbox posts nothing. This means that for an unchecked checkbox, ASP.NET MVC will not be able to find a value to map to the rememberMe parameter, and thus throw an exception at you.

The best way then to map a checkbox to a boolean action parameter, is to just set the value of the checkbox to ‘true’ (since it will only ever be used when the checkbox is checked):

<input type="checkbox" id="rememberMe" name="rememberMe" value="true" />
<label
for="rememberMe">Remember me?</label>

and then use a nullable boolean as the controller action parameter:

public ActionResult Login(string username, string password, bool? rememberMe)
{
    // authenticate...
}

This way, you'll get a 'null' when "Remember me?" is unchecked, and true when it is checked.

Currently rated 5.0 by 9 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Powered by BlogEngine.NET 1.4.5.0

Welcome!

My name is Fredrik Kalseth, and this is my blog - thanks for visiting! I am fortunate enough to work with what I love for a living, and this blog is essentially the biproduct of that.

I work as a senior consultant for Capgemini, and am also an active participant in the Norwegian .NET community, as an avid attendee but also as a speaker (most recently at NNUG and MSDN Live).

As a developer, I have a wide circle of interest. My primary passion is for agile, test-driven development, with focus on best practices and clean code. That said, I also love to work on the frontend, especially with web development.

On Twitter? My handle is fkalseth. On LinkedIn? I`m there too.

Disclaimer

This is a personal blog; any opinions expressed here are my own and do not necessarily reflect those of my employer. All content herein is my own original creation, and as such is protected by copyright law. Unless otherwise stated, all source code posted on this blog is freely usable under the Microsoft Permissive License.

What Readers Talk About

Comment RSS