vendredi 27 mai 2016

Why does Html.CheckBoxFor output a 'false' as the value where a Html.HiddenFor outputs a 'False' (lower vs proper case)?

I'm not sure if it is intended behavior or not but when I use a @Html.CheckBoxFor(x => x.CheckBox1) it will output two input fields (one of which is false and one is true because html doesn't post back false checkboxes). But the actual html rendered is:

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

But when I use a @Html.HiddenFor(x => x.CheckBox2) the output is:

<input id="CheckBox2" name="CheckBox2" type="hidden" value="True">

Note the 'true' and the 'True'. When the out of the box jquery.validate.js tries to use the equalTo function it will never be equal to each other because 'true' !== 'True'.

To get around this I can modify the equalTo method to ignore case when comparing checkbox values but I was just curious if this is a bug with the MVC framework or this was intended behavior?

Here is a fiddle reproducing the issue (view source on the output to view the html):

Edit 1: I have found in the CheckBoxFor source that it is passing a lower case 'true' when building the Checkbox. The question still remains, why lower case instead of proper case. Line 96 of the source.

Aucun commentaire:

Enregistrer un commentaire