1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Text;
  6. using System.Web;
  7. using System.Web.Mvc;
  8.  
  9. namespace Demo
  10. {
  11. public static class CheckBoxListHelper
  12. {
  13. public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, bool isHorizon = true)
  14. {
  15. return CheckBoxList(helper, name, helper.ViewData[name] as IEnumerable<SelectListItem>, new { }, isHorizon);
  16. }
  17.  
  18. public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IEnumerable<SelectListItem> selectList, bool isHorizon = true)
  19. {
  20. return CheckBoxList(helper, name, selectList, new { }, isHorizon);
  21. }
  22.  
  23. public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(this HtmlHelper helper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, bool isHorizon = true)
  24. {
  25. string[] propertys = expression.ToString().Split(".".ToCharArray());
  26. string id = string.Join("_", propertys, , propertys.Length - );
  27. string name = string.Join(".", propertys, , propertys.Length - );
  28.  
  29. return CheckBoxList(helper, id, name, selectList, new { }, isHorizon);
  30. }
  31.  
  32. public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IEnumerable<SelectListItem> selectList, object htmlAttributes, bool isHorizon = true)
  33. {
  34. return CheckBoxList(helper, name, name, selectList, htmlAttributes, isHorizon);
  35. }
  36.  
  37. public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string id, string name, IEnumerable<SelectListItem> selectList, object htmlAttributes, bool isHorizon = true)
  38. {
  39. IDictionary<string, object> HtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
  40.  
  41. HashSet<string> set = new HashSet<string>();
  42. List<SelectListItem> list = new List<SelectListItem>();
  43. string selectedValues = (selectList as SelectList).SelectedValue == null ? string.Empty : Convert.ToString((selectList as SelectList).SelectedValue);
  44. if (!string.IsNullOrEmpty(selectedValues))
  45. {
  46. if (selectedValues.Contains(","))
  47. {
  48. string[] tempStr = selectedValues.Split(',');
  49. for (int i = ; i < tempStr.Length; i++)
  50. {
  51. set.Add(tempStr[i].Trim());
  52. }
  53.  
  54. }
  55. else
  56. {
  57. set.Add(selectedValues);
  58. }
  59. }
  60.  
  61. foreach (SelectListItem item in selectList)
  62. {
  63. item.Selected = (item.Value != null) ? set.Contains(item.Value) : set.Contains(item.Text);
  64. list.Add(item);
  65. }
  66. selectList = list;
  67.  
  68. HtmlAttributes.Add("type", "checkbox");
  69. HtmlAttributes.Add("id", id);
  70. HtmlAttributes.Add("name", name);
  71. HtmlAttributes.Add("style", "border:none;");
  72.  
  73. StringBuilder stringBuilder = new StringBuilder();
  74.  
  75. foreach (SelectListItem selectItem in selectList)
  76. {
  77. IDictionary<string, object> newHtmlAttributes = HtmlAttributes.DeepCopy();
  78. newHtmlAttributes.Add("value", selectItem.Value);
  79. if (selectItem.Selected)
  80. {
  81. newHtmlAttributes.Add("checked", "checked");
  82. }
  83.  
  84. TagBuilder tagBuilder = new TagBuilder("input");
  85. tagBuilder.MergeAttributes<string, object>(newHtmlAttributes);
  86. string inputAllHtml = tagBuilder.ToString(TagRenderMode.SelfClosing);
  87. string containerFormat = isHorizon ? @"<label> {0} {1}</label>" : @"<p><label> {0} {1}</label></p>";
  88. stringBuilder.AppendFormat(containerFormat,
  89. inputAllHtml, selectItem.Text);
  90. }
  91. return MvcHtmlString.Create(stringBuilder.ToString());
  92.  
  93. }
  94. private static IDictionary<string, object> DeepCopy(this IDictionary<string, object> ht)
  95. {
  96. Dictionary<string, object> _ht = new Dictionary<string, object>();
  97.  
  98. foreach (var p in ht)
  99. {
  100. _ht.Add(p.Key, p.Value);
  101. }
  102. return _ht;
  103. }
  104. }
  105. }

MVC CheckBoxList的实现的更多相关文章

  1. MVC生成CheckBoxList并对其验证

    原文:MVC生成CheckBoxList并对其验证 通过扩展方法,可以让CheckBox水平排列,生成CheckBoxList,正如"MVC扩展生成CheckBoxList并水平排列&quo ...

  2. ASP.NET MVC 3 CheckBoxList 的使用

    在以前的 ASP.NET MVC 中可以直接使用 CheckBoxList,但后来不知道什么原因在 MVC 中移除了 CheckBoxList,所以默认情况下 ASP.NET MVC 3 中是没有 C ...

  3. 再议ASP.NET MVC中CheckBoxList的验证

    在ASP.NET MVC 4中谈到CheckBoxList,经常是与CheckBoxList的显示以及验证有关.我在"MVC扩展生成CheckBoxList并水平排列"中通过扩展H ...

  4. MVC扩展HtmlHelper,加入RadioButtonList、CheckBoxList、DropdownList

    代码: using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions ...

  5. MVC扩展生成CheckBoxList并水平排列

    本篇体验生成CheckBoxList的几个思路,扩展MVC的HtmlHelper生成CheckBoxList,并使之水平排开.     通过遍历从控制器方法拿到的Model集合 □ 思路 比如为一个用 ...

  6. MVC中CheckBoxList的3种实现方式

    比如,当为一个用户设置角色的时候,角色通常以CheckBoxList的形式呈现.用户和角色是多对多关系: using System.Collections.Generic; using System. ...

  7. ShenNiu.MVC管理系统

    本篇将要和大家分享的是一个简单的后台管理系统,这里先发个地址http://www.lovexins.com:8081/(登陆账号:youke,密码:123123:高级用户账号:gaoji,密码:123 ...

  8. .NET MVC3中扩展一个HtmlHelper方法CheckBoxList

    MVC中有DropDownList方法,挺好用,可是最常用的需求,一组checkboxlist咋没个类似方法呢?郁闷之余,自己做一个吧,直接上代码 public static MvcHtmlStrin ...

  9. [引]ASP.NET MVC 4 Content Map

    本文转自:http://msdn.microsoft.com/en-us/library/gg416514(v=vs.108).aspx The Model-View-Controller (MVC) ...

随机推荐

  1. 使用 Json.Net 对Json文本进行 增删改查

    JSON 已经成为当前主流交互格式, 如何在C#中使用 Json.Net 对Json文本进行 增删改查呢?见如下代码 #region Create (从零创建) public static strin ...

  2. 常用MySQL图形化管理工具

    MySQL的管理维护工具非常多,除了系统自带的命令行管理工具之外,还有许多其他的图形化管理工具,这里我介绍几个经常使用的MySQL图形化管理工具,供大家参考. MySQL是一个非常流行的小型关系型数据 ...

  3. Spark运行模式与Standalone模式部署

    上节中简单的介绍了Spark的一些概念还有Spark生态圈的一些情况,这里主要是介绍Spark运行模式与Spark Standalone模式的部署: Spark运行模式 在Spark中存在着多种运行模 ...

  4. redis 缓存技术与memcache的区别

    1 什么是redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合)和zset( ...

  5. XIB——AutoLayout添加约束

    仿QQ登录界面: 说明:以下各图背景红色只是方便看清楚: 1.创建工程:创建xib文件,不会,可看上上上篇:初识xib: 2.打开xib文件: (1).创建头像: 拖控件:uiimageview--& ...

  6. git --help出来的命令 + eclipse里用git小记

    用法:git [--version] [--help] [-C <path>] [-c name=value]           [--exec-path[=<path>]] ...

  7. 【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.原因,工作队列被占用,只需 ...

  8. mysql的explain学习

    explain是用来获取sql执行计划的信息. 上面是一个最简单的sql分析.下面来分析每列的意思. ①id  ②select_type ③ table ④type ⑤possible_key ⑥ke ...

  9. jquery 的一些基本操作

    日常使用中的操作记录,持续更新中: ECharts,一个纯 Javascript 的图表库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE8/9/10/11,Chrome,Fire ...

  10. git常用命令

    开始的时候 git config --global user.name "Your Name" git config --global user.email "email ...