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) ...
随机推荐
- SSIS 实例——将SQL获取的信息传递到Email中
最近在为公司财务开发一个邮件通知时遇到了一个技术问题.原来我设计SSIS的是每天将ERP系统支付数据导出到财务支付平台后 Email 通知财务,然后财务到支付平台上进行支付操作.由于那个时候开发时间很 ...
- Mybatis 查不到数据,总是返回Null
mybatis突然查不到数据,查询返回的都是Null,但是 select count(*) from xxx查询数量,返回却是正常的. Preparing: SELECT id,a9004,a9005 ...
- URL_MODEL 2 不能访问 在APACHE服务器上的访问方式上去除index.php
thinkphp URL_MODEL=2,访问链接http://i.cnblogs.com/Online/index.html 报错: Not Found The requested URL /on ...
- __weak与__block区别,深层理解两者区别
准备工作 首先我定义了一个类 MyObject 继承 NSObject,并添加了一个属性 text,重写了description方法,返回 text 的值.这个主要是因为编译器本身对 NSString ...
- java动态编译笔记
1 前言 Java的动态编译知识,真真在实际开发中并不是经常遇到.但是学习java动态编译有助于我们从更深一层次去了解java.对掌握jdk的动态代理模式,这样我们在学习其他一些开源框架的时候就能够知 ...
- C++点滴----关于类常成员函数
关于C++中,类的常成员函数 声明样式为: 返回类型 <类标识符::>函数名称(参数表) const 一些说明: 1.const是函数声明的一部分,在函数的实现部分也需要加上const ...
- MyBatis源码分析-SQL语句执行的完整流程
MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以对配置和原生Map使用简 ...
- [LeetCode] Flip Game 翻转游戏之二
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- [LeetCode] Compare Version Numbers 版本比较
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...
- [LeetCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...