asp.net mvc html.checkboxfor为什么会生成一个的每个checkbox后边都跟一个input hidden呢?

随智阔 发布于 2012/08/01 15:23
阅读 4K+
收藏 0
<%=Html.CheckBox("chk1",true) %> 
<%=Html.CheckBox("chk1", new { @class="checkBox"}) %> 
<%=Html.CheckBoxFor(a =>a.IsVaild, new { @class = "checkBox" })%>
 
生成结果:
 
<input checked="checked" id="chk1" name="chk1" type="checkbox" value="true" /><input name="chk1" type="hidden" value="false" />
 
<input class="checkBox" id="chk1" name="chk1" type="checkbox" value="true" /><input name="chk1" type="hidden" value="false" />
 
<input checked="checked" class="checkBox" id="IsVaild" name="IsVaild" type="checkbox" value="true" /><input name="IsVaild" type="hidden" value="false" />
加载中
0
飞得更高

If checkbox is not selected, form field is not submitted. That is why there is always false value in hidden field. If you leave checkbox unchecked, form will still have value from hidden field. That is how ASP.NET MVC handles checkbox values.

If you want to confirm that, place a checkbox on form not with Html.Hidden, but with <input type="checkbox" name="MyTestCheckboxValue"></input>. Leave checkbox unchecked, submit form and look at posted request values on server side. You'll see that there is no checkbox value. If you had hidden field, it would contain MyTestCheckboxValue entry with false value.

返回顶部
顶部