Htmlhelper—CheckBox自动生成两个input
前言
在之前的一篇文章中小猪分享了Htmlhelper的用法。其中有意思的一个就是Checkbox,有必要单独拿出来讲一讲。
Htmlhelper—CheckBox
细心的读者一定发现了当使用类似语法
@Html.CheckBox("recommend")
生成的html中除了一个 type="checkbox"的表单元素之外另外还生成了一个 type="hidden"的隐藏元素
<input id="recommend" name="recommend" type="checkbox" value="true">
<input name="recommend" type="hidden" value="false">
这两个表单元素都有一个name为"recommend"的属性。type为checkbox的表单元素value为true,type为hidden的表单元素value值为false。
当我们不选中该CheckBox值时通过监听网络请求可以发现请求值
recommend=false
当选中CheckBox值时发现请求值为
recommend=true&recommend=false
看看一个老外的解释:
The HtmlHelper.CheckBox method puts in a hidden value with the same name at the checkbox. The reason this is done is because if the checkbox is not checked, the browswer doesn't send a value for the checkbox with the form post. The way around this is via the hidden variable which results in a value of false if the checkbox is not checked or true,false if it is checked.
It's not confused, it's just reporting the values it has. When you use the CHeckBox helper, the framework spits out two input controls, one for the checkbox itself, and a hidden one with the same name that's value is false. it does this to help the default model binder always have a value for the name in question, because HTML forms don't append cleared checkboxes to the data. So you have two choices. You can either use the method you're currently using, but use string parsing to extract the "first" value (which equates to the boolean value you're looking for) or else you can instead use a custom model object as the input param of the POST action, to let the model binder do it's job.http://forums.asp.net/t/1440058.aspx
通过上面俩个老外的解释我们可以看出:在表单提交时,如果Checkbox不选中的话那么是不会提交该Checkbox的值的,这个时候在后台也就没有办法得到该字段,而MVC默认的给该字段加上了一个隐藏的false值,当前台没选中的话就把false传过去,而选中了的话则把true(Checkbox的值)和false(隐藏域的值)都以相同的name穿过去让后台来处理,再通过ASP.NET MVC自带的模型绑定功能就会自动的将这个bool值转换成模型中的值,很大的方便了我们编程。
如果我们自己手写html代码
<input id="recommend" name="recommend" type="checkbox" checked="checked">
则不能够直接在Controller中使用类似
public ActionResult Create(bool Recommend)
{
// do something
return RedirectToAction("List");
}
这样的代码,因为本身的html input的Checkbox在选中时只传递一个on值(如果没有value属性时),得像下面这样获取才能实现一个很简单的效果,
public ActionResult Create(TB_Book tb_book ,string Recommend)
{
if (Recommend == "on")
{
//do something
}
//do something
return RedirectToAction("List");
}
这样就得把是bool值的字段都单独拿出来写。好麻烦的说!
Htmlhelper—CheckBox自动生成两个input的更多相关文章
- RTImageAssets 自动生成 AppIcon 和 @2x @1x 比例图片
下载地址:https://github.com/rickytan/RTImageAssets 此插件用来生成 @3x 的图片资源对应的 @2x 和 @1x 版本,只要拖拽高清图到 @3x 的位置上,然 ...
- iOS图片攻略之:有3x自动生成2x 1x图片
关键字:Xcode插件,生成图片资源 代码类库:其他(Others) GitHub链接:https://github.com/rickytan/RTImageAssets 本项目是一个 Xc ...
- 根据表结构自动生成JavaBean,史上最强最专业的表结构转JavaBean的工具(第2版)
目录: 第1版:http://blog.csdn.net/vipbooks/article/details/51912143 ...
- 基于数据库的自动化生成工具,自动生成JavaBean、自动生成数据库文档等(v4.1.2版)
目录: 第1版:http://blog.csdn.net/vipbooks/article/details/51912143 第2版:htt ...
- 第二次作业:对Github的初步学习应用(四则运算的自动生成C#实现)
GIT地址 https://github.com/Anzerl?tab=repositories GIT用户名 Anzerl 学号后五位 062426 博客地址 https://www.cnb ...
- make自动生成依赖文件的两种形式
最近编译源文件发现当修改头文件后,make并不会自动把包含此头文件的所有源文件重新编译,而每次都是需要把对应的中间文件清除才能重新编译,非常的麻烦.因此需要make自动对源文件所依赖的头文件进行管理, ...
- 设计数据库 ER 图太麻烦?不妨试试这两款工具,自动生成数据库 ER 图!!!
忙,真忙 点赞再看,养成习惯,微信搜索『程序通事』,关注就完事了! 点击查看更多精彩的文章 这两个星期真是巨忙,年前有个项目因为各种莫名原因,一直拖到这个月才开始真正测试.然后上周又接到新需求,马不停 ...
- 简单两步快速学会使用Mybatis-Generator自动生成entity实体、dao接口和简单mapper映射(用mysql和oracle举例)
前言: mybatis-generator是根据配置文件中我们配置的数据库连接参数自动连接到数据库并根据对应的数据库表自动的生成与之对应mapper映射(比如增删改查,选择性增删改查等等简单语句)文件 ...
- 《音乐商店》第4集:自动生成StoreManager控制器
一.自动生成StoreManager控制器 二.查看 StoreManager 控制器的代码 现在,Store Manager 控制器中已经包含了一定数量的代码,我们从头到尾重新过一下. 1.访问数据 ...
随机推荐
- iOS - UITouch
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UITouch : NSObject @available(iOS 2.0, *) public class UIT ...
- rpm 更新/升级 软件包(libGL-devel手动安装过程)
rpm参数解释 -i 安装 -h 解压rpm的时候打印50个斜条 (#) -v 显示详细信息 升级命令rpm -Uvh rpm文件名 参数解释 -U 升级 -h 解压rpm的时候打印50个斜条 (#) ...
- Dolphin for Android(v11.5.1[Jetpack:内置])
1. 下载的地址为“http://www.techspot.com/downloads/5927-dolphin-browser-for-android.html” ZC: 由于 Google Pla ...
- ubuntu 14.0.04 通过自启动脚本实现主机和虚拟机的文件夹共享
首先要先设置主机ip为固定ip: 1.vi /etc/rc.local,进入vi界面,在尾部增加一行代码: mount -t cifs -o username=share,password=12345 ...
- python paramiko模块SSH自动登录linux系统进行操作
1). Linux系统首先要开启SSH服务:service ssh status 如果没安装的话,则要:apt-get install openssh-server service ssh resta ...
- Mybatis 的分页插件PageHelper-4.1.1的使用
Mybatis 的分页插件 PageHelper 项目地址:http://git.oschina.net/free/Mybatis_PageHelper 文档地址:http://git.oschin ...
- 转:C/C++中,空数组、空类、类中空数组的解析及其作用
转自:http://blog.sina.com.cn/s/blog_93b45b0f01015s95.html 我们经常会遇到这些问题: (1)C++中定义一个空类,他们它的大小(sizeof) 为多 ...
- form in drupal
qin_form_ajax_example_form($form, &$form_state)类似函数的参数永远都是一样的,最多把$form前面也加上& 当没有实现页面跳转时,$for ...
- 树 - 从零开始实现by C++
参考链接:数据结构探险之树篇
- 串行通讯之Qt
目录 第1章 Qt 串行通讯 1 1.1 配置.pro文件 1 1.2 查询串口信息 1 1.3 配置.打开串口 3 1.4 setRequestToSend在Windows上 ...