The problem is Razor (MVC 5) renders out HiddenFor and Hidden helper incorrectly.
I have a view model like this:
public class MyViewModel
{
public bool BoolValueInsideViewModel { get; set; }
public MyViewModel()
{
BoolValueInsideViewModel = false;
}
}
and controller is simple as:
model = new MyViewModel();
model.BoolValueInsideViewModel = false;
return PartialView("_MyView", model);
Second line is just to be sure value is set to false. View looks like this:
@model MyViewModel
@Html.Hidden("BoolValueInsideViewModel", false)
@Html.Hidden("BoolValueNotInViewModel", false)
But on browser I get this:
<input data-val="true" data-val-required="The BoolValueInsideViewModel field is required." id="BoolValueInsideViewModel" name="BoolValueInsideViewModel" type="hidden" value="true">
<input id="BoolValueNotInViewModel" name="BoolValueNotInViewModel" type="hidden" value="False">
Note that I have like 15 other view models and controller methods in same controller works fine with same code (I use HiddenFor with strongly typed values normaly, here I changed to Hidden to show that it does not work even with hard coded value in view).
Putting a break point on view, shows model is actually has BoolValueInsideViewModel equal to false, but result on browser is different. Also I get the result via JQuery post callback, so I checked raw string returning from AJAX function and the value there is also "True" instead of false.
What I tried:
- Clean up project and rebuild!
- Renaming property
- Renaming view model, even renaming file containing view model
- Renaming view
I know! It looks stupid and the most simplest thing on ASP.NET MVC, but I spent half a day on it. Other view models and partial views render correctly.
The weird thing is I don't have any data annotation on view model, but why it includes data validation attributes on the HTML element?