2

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): https://dotnetfiddle.net/LxOm3J

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. https://www.symbolsource.org/Public/Metadata/NuGet/Project/Microsoft.AspNet.Mvc/4.0.20710.0/Release/.NETFramework,Version=v4.0/System.Web.Mvc/System.Web.Mvc/System.Web.Mvc/Html/InputExtensions.cs Line 96 of the source.

Beardo08
  • 76
  • 1
  • 8
  • The `HiddenFor()` method calls the `.ToString()` method of the property value which returns one of the constants `TrueString` ("True") or `FalseString` ("False"). As to why the MVC team chose to return lowercase "true" in the `CheckBoxFor()` method is only something they could answer. (refer also [these answers](http://stackoverflow.com/questions/491334/why-does-boolean-tostring-output-true-and-not-true) for more detail) –  May 28 '16 at 01:17
  • Its not really clear what you mean by _the out of the box jquery.validate.js tries to use the equalTo function ..._. What validation rules have you added and why would you want to be comparing `'true'` and `'True'` (are you trying to implement some kind of conditional validation based on the value of the hidden input)? –  May 28 '16 at 01:18

0 Answers0