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

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

问题描述

我在我的视图页面上写了以下代码;

I have written the below code on my view page;

@Html.CheckBox("ChxName",true)

我得到了以下结果;

<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?

推荐答案

未选中的复选框不会发布,因此隐藏字段(设置为 false)允许模型绑定仍然有效.

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

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

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