ASP.NET MVC 5 renders different bool value for hidden input

て烟熏妆下的殇ゞ 提交于 2019-11-30 16:58:11
Jason Evans

I think I've figured it out.

I believe the Razor viewengine is adhering to the HTML 5 way of setting boolean attributes, as described here:

What does it mean in HTML 5 when an attribute is a boolean attribute?

In HTML 5, a bool attribute is set like this:

<input readonly />

or

<input readonly="readonly" />

So the Razor viewengine takes your model's bool value and will render (in my case) the value attribute if Model.IsBoolValue is true. Otherwise, if it's false then the value attribute is not rendered at all.

EDIT:

As mentioned Zabavsky in the comments, to force the value of True or False to appear in the value attrbiute, simple use ToString():

<input type="hidden" value="@Model.BoolProperty.ToString()" />

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!