我有一个复选框和按钮:
@using(Html.BeginForm())
{
<div id="search-block">
<input type="checkbox" name="showAll" id="showAll">Include completed
<input type="submit" value="Apply"/>
</div>
}
还有控制器中的第二件事:
[HttpPost]
public ActionResult Index(FormCollection collection)
{
var showAll = collection["showAll"];
TempData["showAll"] = showAll;
...
something
...
}
它实际上在工作,但是:
如果未选中复选框,则表示我收到null(不会打扰我很多)。
如果选中了复选框,则表示我从中接收到“ on” FormCollection
,这不是我所需要的。我想收到是非题。
我怎样才能做到这一点?
<input type="checkbox" name="showAll" id="showAll">Include completed
将发布"on"
或的值"off"
。
这个WONT绑定到一个布尔值,
要接收布尔值,可以使用HTML Helper Checkbox Like
@Html.CheckBox("showAll")
<label for="showAll" style="display:inline">Include completed</label>
更新:
如果选中,则html helper将回发
"true,false"
,如果未选中,则为"false"
您可以采用以下解决方法:
bool MyBoolValue= Convert.ToBoolean(collection["showAll"].Split(',')[0]);
这是因为Html帮助器类添加了一个额外的hidden
字段来保存复选框的状态
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句