asp.net mvc的:为什么Html.CheckBox产生额外的隐藏输入 [英] asp.net mvc: why is Html.CheckBox generating an additional hidden input
问题描述
我只注意到 Html.CheckBox(富)
2产生输入,而不是一个,有人知道为什么会这样?
<输入ID =富NAME =foo的类型=复选框VALUE =真/>
<输入名称=foo的类型=隐藏值=FALSE/>
如果未选中复选框,表单字段不提交。这就是为什么总有隐藏字段值为false。如果您离开复选框选中,形式仍具有隐藏字段值。这就是ASP.NET MVC如何处理复选框值。
如果您想确认的是,放置在不与Html.Hidden形式的复选框,但<输入类型=复选框NAME =MyTestCheckboxValue>< /输入>
。离开复选框选中,提交表单,并期待在发布请求值在服务器端。你会看到,没有复选框值。如果您已经隐藏字段,它将包含与
值。 MyTestCheckboxValue
项虚假
I just noticed that Html.CheckBox("foo")
generates 2 inputs instead of one, anybody knows why is this so ?
<input id="foo" name="foo" type="checkbox" value="true" />
<input name="foo" type="hidden" value="false" />
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.
这篇关于asp.net mvc的:为什么Html.CheckBox产生额外的隐藏输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!