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.访问数据 ...
随机推荐
- Log4j XML 配置
Xml代码 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configurat ...
- 2013 Multi-University Training Contest 6
HDU-4655 Cut Pieces 题意:有N个格子能够被涂色,每个格子能够涂1-ai 种颜色,当N=6,涂色方案:112233 认为方案中共有3个颜色块:涂色方案:121212 认为方案中共有6 ...
- iOS - ImageCache 网络图片缓存
1.ImageCache 使用内存缓存方式: 使用沙盒缓存方式: 使用网络图片第三方库方式: SDWebImage: iOS 中著名的网络图片处理框架 包含的功能:图片下载.图片缓存.下载进度监听.g ...
- mysql概要(十二)事务
1.特性 2.开启结束取消事务:需要选择支持事务的引擎 3,隐式提交事务: 4.事务的隔离级别:
- ccc
课本第291页第4题 #include<stdio.h> void main() { int n, m, i, k; int p_begin; ]; scanf("%d" ...
- spring事务知识
事务的传播行为? 在Spring 的事务中, _可以通过 propagation 来定义事务的传播行为_: PROPAGATION_required:如果当前没有事务,就新建一个事务,如果已经存在一个 ...
- Android Menu菜单使用
如上图右上角,菜单选项的编辑,第一种代码实现方式如下: package com.example.menu; import android.os.Bundle; import android.app.A ...
- Java编程思想学习笔记_5(IO流)
一.用DataInputStream读取字符 可以使用available方法查看还有多少可供存取的字符.示例如下: public class Test1 { public static void ma ...
- 【Todo】Java新技术学习笔记-from某技术分析
看到这篇文章:http://mt.sohu.com/20160806/n462923089.shtml <十余年技术大牛告诉你,这些Java新技术不可错过> 虽然讲的比较泛,但是里面提到的 ...
- [css] line-height
原文:http://www.zhangxinxu.com/wordpress/2009/11/css%E8%A1%8C%E9%AB%98line-height%E7%9A%84%E4%B8%80%E4 ...