为什么我隐藏输入文字:价值="价值"而不是真/假? [英] Why is my hidden input writing: value="value" instead of true/false?
问题描述
我有一个MVC4部位,用(作为一个隐藏的表单的一部分):
I have an MVC4 site, with (as part of a hidden form):
<input name="somefield" type="hidden" value="@ViewBag.Test"/>
ViewBag.Test
的值是真正
。表单域发布到表单的输入参数:
The value of ViewBag.Test
is true
. The form field is posting to an input parameter of the form:
public ActionResult SomeAction(bool somefield = false, ...)
但 somefield
总是假
。经调查,我看到源$ C $ c的:
but somefield
is always false
. Upon investigating, I see that the source code has:
<input name="somefield" type="hidden" value="value"/>
不过,我的知道的这个用来工作。发生了什么事,我该怎么办?
However, I know this used to work. What has happened, and what can I do?
推荐答案
此行为MVC3和MVC4之间变化。在MVC3,如果您有:
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"/>
然而,在MVC4,它遵循复选框等规则,因此,如果该值为真正
您可以:
<input name="somefield" type="hidden" someprop="someprop"/>
如果是假
它完全忽略:
<input name="somefield" type="hidden"/>
要解决这个问题,可以考虑的ToString()
:
To get around this, consider .ToString()
:
<input name="somefield" type="hidden"
someprop="@(SomeBooleanExpression.ToString())"/>
然后如下字符串规则,而不是布尔规则。
which then follows string rules rather than boolean rules.
这篇关于为什么我隐藏输入文字:价值=&QUOT;价值&QUOT;而不是真/假?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!