MVC CheckBoxList的实现
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Text;
- using System.Web;
- using System.Web.Mvc;
- namespace Demo
- {
- public static class CheckBoxListHelper
- {
- public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, bool isHorizon = true)
- {
- return CheckBoxList(helper, name, helper.ViewData[name] as IEnumerable<SelectListItem>, new { }, isHorizon);
- }
- public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IEnumerable<SelectListItem> selectList, bool isHorizon = true)
- {
- return CheckBoxList(helper, name, selectList, new { }, isHorizon);
- }
- public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(this HtmlHelper helper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, bool isHorizon = true)
- {
- string[] propertys = expression.ToString().Split(".".ToCharArray());
- string id = string.Join("_", propertys, , propertys.Length - );
- string name = string.Join(".", propertys, , propertys.Length - );
- return CheckBoxList(helper, id, name, selectList, new { }, isHorizon);
- }
- public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IEnumerable<SelectListItem> selectList, object htmlAttributes, bool isHorizon = true)
- {
- return CheckBoxList(helper, name, name, selectList, htmlAttributes, isHorizon);
- }
- public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string id, string name, IEnumerable<SelectListItem> selectList, object htmlAttributes, bool isHorizon = true)
- {
- IDictionary<string, object> HtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
- HashSet<string> set = new HashSet<string>();
- List<SelectListItem> list = new List<SelectListItem>();
- string selectedValues = (selectList as SelectList).SelectedValue == null ? string.Empty : Convert.ToString((selectList as SelectList).SelectedValue);
- if (!string.IsNullOrEmpty(selectedValues))
- {
- if (selectedValues.Contains(","))
- {
- string[] tempStr = selectedValues.Split(',');
- for (int i = ; i < tempStr.Length; i++)
- {
- set.Add(tempStr[i].Trim());
- }
- }
- else
- {
- set.Add(selectedValues);
- }
- }
- foreach (SelectListItem item in selectList)
- {
- item.Selected = (item.Value != null) ? set.Contains(item.Value) : set.Contains(item.Text);
- list.Add(item);
- }
- selectList = list;
- HtmlAttributes.Add("type", "checkbox");
- HtmlAttributes.Add("id", id);
- HtmlAttributes.Add("name", name);
- HtmlAttributes.Add("style", "border:none;");
- StringBuilder stringBuilder = new StringBuilder();
- foreach (SelectListItem selectItem in selectList)
- {
- IDictionary<string, object> newHtmlAttributes = HtmlAttributes.DeepCopy();
- newHtmlAttributes.Add("value", selectItem.Value);
- if (selectItem.Selected)
- {
- newHtmlAttributes.Add("checked", "checked");
- }
- TagBuilder tagBuilder = new TagBuilder("input");
- tagBuilder.MergeAttributes<string, object>(newHtmlAttributes);
- string inputAllHtml = tagBuilder.ToString(TagRenderMode.SelfClosing);
- string containerFormat = isHorizon ? @"<label> {0} {1}</label>" : @"<p><label> {0} {1}</label></p>";
- stringBuilder.AppendFormat(containerFormat,
- inputAllHtml, selectItem.Text);
- }
- return MvcHtmlString.Create(stringBuilder.ToString());
- }
- private static IDictionary<string, object> DeepCopy(this IDictionary<string, object> ht)
- {
- Dictionary<string, object> _ht = new Dictionary<string, object>();
- foreach (var p in ht)
- {
- _ht.Add(p.Key, p.Value);
- }
- return _ht;
- }
- }
- }
MVC CheckBoxList的实现的更多相关文章
- MVC生成CheckBoxList并对其验证
原文:MVC生成CheckBoxList并对其验证 通过扩展方法,可以让CheckBox水平排列,生成CheckBoxList,正如"MVC扩展生成CheckBoxList并水平排列&quo ...
- ASP.NET MVC 3 CheckBoxList 的使用
在以前的 ASP.NET MVC 中可以直接使用 CheckBoxList,但后来不知道什么原因在 MVC 中移除了 CheckBoxList,所以默认情况下 ASP.NET MVC 3 中是没有 C ...
- 再议ASP.NET MVC中CheckBoxList的验证
在ASP.NET MVC 4中谈到CheckBoxList,经常是与CheckBoxList的显示以及验证有关.我在"MVC扩展生成CheckBoxList并水平排列"中通过扩展H ...
- MVC扩展HtmlHelper,加入RadioButtonList、CheckBoxList、DropdownList
代码: using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions ...
- MVC扩展生成CheckBoxList并水平排列
本篇体验生成CheckBoxList的几个思路,扩展MVC的HtmlHelper生成CheckBoxList,并使之水平排开. 通过遍历从控制器方法拿到的Model集合 □ 思路 比如为一个用 ...
- MVC中CheckBoxList的3种实现方式
比如,当为一个用户设置角色的时候,角色通常以CheckBoxList的形式呈现.用户和角色是多对多关系: using System.Collections.Generic; using System. ...
- ShenNiu.MVC管理系统
本篇将要和大家分享的是一个简单的后台管理系统,这里先发个地址http://www.lovexins.com:8081/(登陆账号:youke,密码:123123:高级用户账号:gaoji,密码:123 ...
- .NET MVC3中扩展一个HtmlHelper方法CheckBoxList
MVC中有DropDownList方法,挺好用,可是最常用的需求,一组checkboxlist咋没个类似方法呢?郁闷之余,自己做一个吧,直接上代码 public static MvcHtmlStrin ...
- [引]ASP.NET MVC 4 Content Map
本文转自:http://msdn.microsoft.com/en-us/library/gg416514(v=vs.108).aspx The Model-View-Controller (MVC) ...
随机推荐
- 使用 Json.Net 对Json文本进行 增删改查
JSON 已经成为当前主流交互格式, 如何在C#中使用 Json.Net 对Json文本进行 增删改查呢?见如下代码 #region Create (从零创建) public static strin ...
- 常用MySQL图形化管理工具
MySQL的管理维护工具非常多,除了系统自带的命令行管理工具之外,还有许多其他的图形化管理工具,这里我介绍几个经常使用的MySQL图形化管理工具,供大家参考. MySQL是一个非常流行的小型关系型数据 ...
- Spark运行模式与Standalone模式部署
上节中简单的介绍了Spark的一些概念还有Spark生态圈的一些情况,这里主要是介绍Spark运行模式与Spark Standalone模式的部署: Spark运行模式 在Spark中存在着多种运行模 ...
- redis 缓存技术与memcache的区别
1 什么是redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合)和zset( ...
- XIB——AutoLayout添加约束
仿QQ登录界面: 说明:以下各图背景红色只是方便看清楚: 1.创建工程:创建xib文件,不会,可看上上上篇:初识xib: 2.打开xib文件: (1).创建头像: 拖控件:uiimageview--& ...
- git --help出来的命令 + eclipse里用git小记
用法:git [--version] [--help] [-C <path>] [-c name=value] [--exec-path[=<path>]] ...
- 【svn】在提交文件是报错:previous operation has not finished;run 'cleanup' if it was interrupted
1.svn在提交文件是报错:previous operation has not finished;run 'cleanup' if it was interrupted2.原因,工作队列被占用,只需 ...
- mysql的explain学习
explain是用来获取sql执行计划的信息. 上面是一个最简单的sql分析.下面来分析每列的意思. ①id ②select_type ③ table ④type ⑤possible_key ⑥ke ...
- jquery 的一些基本操作
日常使用中的操作记录,持续更新中: ECharts,一个纯 Javascript 的图表库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE8/9/10/11,Chrome,Fire ...
- git常用命令
开始的时候 git config --global user.name "Your Name" git config --global user.email "email ...