Asp – Html.Checkbox does not preserve its state in ASP.net MVC

asp.net-mvccheckbox

I have this annoying issue with the checkbox created using the Html.Checkbox extension method. Apparently the state of the checkbox is not saved in case of a postback (due to a form validation error). Delving into the MVC code itself (System.Web.Mvc.Html.InputExtensions) I found out that the 'checked' property is determined by calling htmlHelper.EvalBoolean(name).

This method looks for the key (specified in the name argument) of the ViewData itself. The problem is that the value of the checkbox is actually located in the ModelState. Calling
htmlHelper.GetModelStateValue(name, typeof(bool)) would return the expected result.

Is this a flaw in the checkbox implementation ?

Best Solution

I posted another workaraound here:

How to maintain state of Html.CheckBox() in ASP.NET MVC