I have a View that contains the following Line of code:
//(DaysOfWeek is a bool[])
@Html.CheckBoxFor(m => m.Data.DaysOfWeek[0])
It starts off as false. When the user “checks” the box and returns, it returns a value for both true and false;
Here is what is being passed back as part of the form data
Data.DaysOfWeek[0]:true
Data.DaysOfWeek[0]:false
Why is it doing that?
This is because standard HTML checkboxes return no value if unchecked. To make this annoying behaviour more intuitive, the
CheckBoxFormethod creates a checkbox and a hidden control with the same name, with a value offalse, something like this:What you will see when the form is posted is either:
Therefore, to test if the box was checked you should use
Contains('True'):