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

后端 未结 1 1891
太阳男子
太阳男子 2021-01-03 18:30

Given the following viewmodel:

public class FooViewModel
{
    public bool IsBoolValue { get; set; }
}

and this view:



        
1条回答
  •  無奈伤痛
    2021-01-03 19:08

    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:

    or

    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():

    0 讨论(0)
提交回复
热议问题