17 Feb 2012 - Digital Strategy // By Productive Edge Team

ASP.NET MVC CheckBoxFor explained

I have had a number of people ask about the CheckBoxFor method, and why it renders a hidden input tag with a value of false alongside the checkbox input. Here's an example:

@Html.CheckBoxFor(m => m.IsEnabled)

will render

<input id="IsEnabled" type="checkbox" value="true" name="IsEnabled">
<input type="hidden" value="false" name="IsEnabled">

The reason this is done is because a form will not post anything to the server for an unchecked checkbox. In ASP.NET MVC, most of the time you are going to want a "false" value to be posted to the model binder, so the hidden value of false is there so that a value is posted back whether the checkbox is checked or not. If the checkbox is not checked, the form value for "IsEnabled" will be "false", and if it is checked, both values will be posted as "true,false". The model binder understands this, and will automatically take the true value from the collection of values. I have read that Ruby on Rails and MonoRail also use a similar technique.

ASP.NET MVC CheckBoxFor explained