I’m using Html.Checkbox("Visible") for displaying a check box to user. In post back, FormCollection["Visible"] value is “true, false”. Why?
in view:
<td>
<%: Html.CheckBox("Visible") %>
</td>
in controller:
adslService.Visible = bool.Parse(collection["Visible"]);
That’s because the
CheckBoxhelper generates an additional hidden field with the same name as the checkbox (you can see it by browsing the generated source code):So both values are sent to the controller action when you submit the form. Here’s a comment directly from the ASP.NET MVC source code explaining the reasoning behind this additional hidden field:
Instead of using
FormCollectionI would recommend you using view models as action parameters or directly scalar types and leave the hassle of parsing to the default model binder: