剃刀视图引擎HTML.Checkbox方法创建一个隐藏的输入。为什么? [英] Razor ViewEngine HTML.Checkbox method creates a hidden input. Why?

查看:136
本文介绍了剃刀视图引擎HTML.Checkbox方法创建一个隐藏的输入。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写我的视图页上的低于code;

I have written the below code on my view page;

@Html.CheckBox("ChxName",true)

和我得到了以下的结果;

and I got the following result;

<input checked="checked" id="ChxName" name="ChxName" type="checkbox" value="true" />
<input name="ChxName" type="hidden" value="false" />

为什么有命名为同一复选框中一个输入元素?

why is there a input element named as the same with checkbox?

推荐答案

未选中复选框不贴,所以隐藏字段(设定为假)允许模型绑定仍然工作。

Unchecked checkboxes are not posted, so the hidden field (set as false) allows the model binding to still work.

只看帖子后面的Request.Form。如果复选框被选中,您将看到:

Look at Request.Form on the post back. If the checkbox is checked, you'll see:

ChxName=true&ChxName=false

模型绑定使用的第一个值。

The model binder uses the first value.

和,如果没有选中该框,你会看到:

and, if the box isn't checked, you'll see:

ChxName=false

这篇关于剃刀视图引擎HTML.Checkbox方法创建一个隐藏的输入。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆