Why is my hidden input writing: value=“value” instead of true/false?

后端 未结 1 1586
傲寒
傲寒 2020-12-15 15:02

I have an MVC4 site, with (as part of a hidden form):


The value o

相关标签:
1条回答
  • 2020-12-15 15:48

    This behaviour changed between MVC3 and MVC4. In MVC3, if you have:

    <input name="somefield" type="hidden" someprop="@(SomeBooleanExpression)"/>
    

    it would write very literally:

    <input name="somefield" type="hidden" someprop="True"/>
    

    However, in MVC4, it follows the "checkbox" etc rules, so if the value is true you get:

    <input name="somefield" type="hidden" someprop="someprop"/>
    

    and if it is false it is omitted completely:

    <input name="somefield" type="hidden"/>
    

    To get around this, consider .ToString():

    <input name="somefield" type="hidden"
       someprop="@(SomeBooleanExpression.ToString())"/>
    

    which then follows string rules rather than boolean rules.

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