码迷,mamicode.com
首页 > Web开发 > 详细

MVC中CheckBox

时间:2014-11-04 14:34:07      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   ar   os   for   sp   strong   文件   

一、单个Checkbox

 

1、View文件

<%= Html.CheckBoxFor(model => model.IsNeverExpired)%>

 

2、生成的HTML为

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

 

3、备注

 

(1)、上面生成的HTML有点奇怪, 为什么它会生成两个控件呢?

因为如果只生成一个input, 系统无法区分"没有选中checkbox" 和 "checkbox没有生成".

 

(2)、那又是为什么只生成一个input就无法区分呢?

原来当一个form中包含checkbox时,如果没有给它赋值(选中), 在服务器端Request.Form中得到的值是NULL

 

 

二、多个Checkbox

 

方法一:通过在视图页编码的方式

 
C# 代码   复制
bubuko.com,布布扣
bubuko.com,布布扣@using MvcCheckBoxList.Model
bubuko.com,布布扣@model MvcApplication2.Models.UserVm
bubuko.com,布布扣
bubuko.com,布布扣@{
bubuko.com,布布扣    ViewBag.Title = "Index";
bubuko.com,布布扣    Layout = "~/Views/Shared/_Layout。cshtml";
bubuko.com,布布扣}
bubuko.com,布布扣
bubuko.com,布布扣@using (Html.BeginForm())
bubuko.com,布布扣{
bubuko.com,布布扣    @Html.HiddenFor(m => m.User.Id)
bubuko.com,布布扣
bubuko.com,布布扣    <br/>
bubuko.com,布布扣    @Html.LabelFor(m => m.User.Name)
bubuko.com,布布扣    @Html.EditorFor(m => m.User.Name)
bubuko.com,布布扣    @Html.ValidationMessageFor(m => m.User.Name)
bubuko.com,布布扣    <br/>
bubuko.com,布布扣    <ul style="list-style:none;">
bubuko.com,布布扣
bubuko.com,布布扣        @foreach (var a in Model.AllRoles)
bubuko.com,布布扣        {
bubuko.com,布布扣            <li>
bubuko.com,布布扣                @if (Model.SelectedRoleIds.Contains(a.Id))
bubuko.com,布布扣                {
bubuko.com,布布扣                    <input type="checkbox" name="SelectedRoleIds" value="@a.Id" id="@a.Id" checked="checked"/>
bubuko.com,布布扣                    <label for="@a.Id">@a.Name</label>
bubuko.com,布布扣                }
bubuko.com,布布扣                else
bubuko.com,布布扣                {
bubuko.com,布布扣                    <input type="checkbox" name="SelectedRoleIds" value="@a.Id" id="@a.Id" />
bubuko.com,布布扣                    <label for="@a.Id">@a.Name</label>
bubuko.com,布布扣                }
bubuko.com,布布扣            </li>
bubuko.com,布布扣        }
bubuko.com,布布扣    </ul>
bubuko.com,布布扣    <br/>
bubuko.com,布布扣    <input type="submit" value="为用户设置角色"/>
bubuko.com,布布扣}
bubuko.com,布布扣
bubuko.com,布布扣@section scripts
bubuko.com,布布扣{
bubuko.com,布布扣    @Scripts.Render("~/bundles/jqueryval")
bubuko.com,布布扣}
bubuko.com,布布扣

 

方法二:通过NuGet的MvcCheckBoxList扩展

→工具--库程序包管理器--程序包管理器控制台→install-package MvcCheckBoxList

 
C# 代码   复制
bubuko.com,布布扣
bubuko.com,布布扣@using MvcCheckBoxList.Model
bubuko.com,布布扣@model MvcApplication2.Models.UserVm
bubuko.com,布布扣
bubuko.com,布布扣@{
bubuko.com,布布扣    ViewBag.Title = "Index";
bubuko.com,布布扣    Layout = "~/Views/Shared/_Layout。cshtml";
bubuko.com,布布扣}
bubuko.com,布布扣
bubuko.com,布布扣@using (Html.BeginForm())
bubuko.com,布布扣{
bubuko.com,布布扣    @Html.HiddenFor(m => m.User.Id)
bubuko.com,布布扣
bubuko.com,布布扣    <br/>
bubuko.com,布布扣    @Html.LabelFor(m => m.User.Name)
bubuko.com,布布扣    @Html.EditorFor(m => m.User.Name)
bubuko.com,布布扣    @Html.ValidationMessageFor(m => m.User.Name)
bubuko.com,布布扣    <br/>
bubuko.com,布布扣    @Html.CheckBoxListFor(m => m.SelectedRoleIds,
bubuko.com,布布扣                            m => m.AllRoles, //所有角色
bubuko.com,布布扣                            r => r.Id, //value值
bubuko.com,布布扣                            r => r.Name, //显示值
bubuko.com,布布扣                            r => r.UserRoles, //用户当前角色
bubuko.com,布布扣                            Position.Horizontal //CheckboxList排列方向
bubuko.com,布布扣                          )
bubuko.com,布布扣    <br/>
bubuko.com,布布扣    <input type="submit" value="为用户设置角色"/>
bubuko.com,布布扣}
bubuko.com,布布扣
bubuko.com,布布扣@section scripts
bubuko.com,布布扣{
bubuko.com,布布扣    @Scripts.Render("~/bundles/jqueryval")
bubuko.com,布布扣}
bubuko.com,布布扣

 

MVC中CheckBox

标签:style   http   io   ar   os   for   sp   strong   文件   

原文地址:http://www.cnblogs.com/rrxc/p/4073430.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!