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.访问数据 ...
随机推荐
- LTE Module User Documentation(翻译4)—— 使用 Fading Trace
LTE用户文档 (如有不当的地方,欢迎指正!) 7 使用 Fading Trace 本节描述如何在 LTE 仿真中使用 fading traces . (1)生成 Fading Traces ...
- [转载] tmux的使用tips
原文: http://tangosource.com/blog/a-tmux-crash-course-tips-and-tweaks/
- 控制反转和spring在项目中可以带来的好处
Spring实例化Bean的三种方式分别是: 1,xml配置使用bean的类构造器 <bean id="personService" class="cn.servi ...
- 基础2 JVM
1. 内存模型以及分区,需要详细到每个区放什么. //运行时数据区域 方法区 Method Area 各个线程共享的内存区域 存储已被虚拟机加载的类信息 常量 静态变量 即时编译器编译后的代码 虚拟机 ...
- 本地存储 localStorage/sessionStorage/cookie
cookie是个基础的东西.是服务器发送到客户端,存储在客户端的一小段数据.可以存储一些配置信息,客户标识信息等.用户下次访问这个网站时,会把上次网站发来的cookie一同发送回去.cookie保存在 ...
- Java开发中的一些小技巧
原文:http://www.cnblogs.com/xdp-gacl/p/3490276.html 一. Java获取URL地址中传递的参数 /** * 获取URL中的参数名和参数值的Map集合 * ...
- Spring Boot 以 jar 包方式运行在后台
spring-boot jar 包方式启动: 首先,为了防止和常用的 Tomcat 8080 端口冲突,将 Spring-boot 项目的端口号设置为 9090. 具体方法:在 application ...
- maven Connection refused: connect
现象: 本地可以访问错误提示中的地址.但使用maven无法下载jar包. 环境: 浏览器上网需要使用代理 解决方法: 设置成正常代理可以.具体方法可以下载一个代理工具.只要IE配置成能直接访问http ...
- Css_Backgroud-position(背景图片)定位问题详解
background-position的说明: 设置或检索对象的背景图像位置.必须先指定 background-image 属性.该属性定位不受对象的补丁属性( padding )设置影响. ...
- python语法笔记(三)
1. 动态类型 python的变量不需要声明,在赋值时,变量可以赋值为任意的值.这和Python的动态类型语言相关. python对象是存在于内存中的实体,代码中写对象名,只是指向该对象的引用.引用和 ...