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的实现的更多相关文章

  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. 从0到1打造直播 App

    转自http://dev.qq.com/topic/5811d42e7fd6ec467453bf58 概要 分享内容: 互联网内容载体变迁历程,文字——图片/声音——视频——VR/AR——……..从直 ...

  2. C#设计模式:原型模式(Prototype)及深拷贝、浅拷贝

    原型模式(Prototype) 定义: 原型模式:用原型实例指定创建对象的种类,并且通过复制这些原型创建新的对象.被复制的实例被称为原型,这个原型是可定制的. Prototype Pattern也是一 ...

  3. 实时跟踪log变化的工具Apachetop

    作为一个网站管理员,我们经常会有需要知道当前什么人正在访问我们的网站,谁正在频繁的抓取我们网站的内容,什么搜索引擎正在抓取我们网站?面对这些问题,我们虽然可以去查看log日志文件,但是却不能让我们实时 ...

  4. MTU(Maximum transmission unit) 最大传输单元

    最大传输单元(Maximum transmission unit),以太网MTU为1500. 不同网络MTU如下: 如果最大报文数据大小(MSS)超过MTU,则会引起分片操作.   路径MTU: 网路 ...

  5. IIS将错误信息发送到浏览器

    本文版权归博客园和dige1993所有,访问作者博客:http://www.cnblogs.com/dige1993 最近又开始玩ASP了,调试的时候出现错误不清楚详细错误信息特别不方便,记得以前可以 ...

  6. Microsoft SQL Server 2008 R2 安装卸载

    问题 问题1 标题: Microsoft SQL Server 2008 R2 安装程序 ------------------------------ 出现以下错误: Could not open k ...

  7. LK产品如何提高通信速率

    之前做LK,由于采用自定义协议,485通信.为了避免通信撞车,采用主控轮询.从机应答的通信方式. 当设备规模扩大,需要轮询的子设备和参数变多时,轮询周期就会加长,人机交互时,系统反应缓慢. 分析主控轮 ...

  8. 个人CTF资源聚合

    i春秋 幻泉 CTF入门课程笔记 视频地址 能力 思维能力 快速学习能力 技术能力 基础 编程基础 (c语言 汇编语言 脚本语言) 数学基础 (算法 密码学) 脑洞 (天马行空的想象推理) 体力耐力( ...

  9. Intellij IDEA 一些不为人知的技巧

    Intellij IDEA 一些不为人知的技巧 2016/12/06 | 分类: 基础技术 | 0 条评论 | 标签: IntelliJ 分享到:38 原文出处: khotyn 今天又听了 Jetbr ...

  10. Ajax中POST和GET的区别

    Get和Post都是向服务器发送的一种请求,只是发送机制不同. 1. GET请求会将参数跟在URL后进行传递,而POST请求则是作为HTTP消息的实体内容发送给WEB服务器.当然在Ajax请求中,这种 ...