MVC扩展HtmlHelper实现CheckBoxList
类别:编程学习 浏览量:1877
时间:2016-4-1 MVC扩展HtmlHelper实现CheckBoxList
MVC扩展HtmlHelper实现CheckBoxListCheckBoxList在Web页面应用非常广泛,在传统的ASP.NET aspx中系统自带有CheckBoxList控件,但是在MVC Razer中没有提供,下面介绍通过扩展HtmlHelper实现CheckBoxList的方法
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<TModel> helper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, bool isHorizon = true)
{
string[] propertys = expression.ToString().Split(".".ToCharArray());
string id = string.Join("_", propertys, 1, propertys.Length - 1);
string name = string.Join(".", propertys, 1, propertys.Length - 1);
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 = 0; 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.IDictionaryCopy();
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> IDictionaryCopy(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;
}
}
调用方式
@Html.CheckBoxList("保存SelectList的ViewData名称") 或者 @Html.CheckBoxList("CheckBoxList名称",new SelectList(IEnumerable items, string dataValueField, string dataTextField, object selectedValue),是否水平显示) 或者 @Html.CheckBoxList("CheckBoxList名称",new SelectList(IEnumerable items, string dataValueField, string dataTextField, object selectedValue),其它html属性,是否水平显示)
@Html.CheckBoxListFor(m => m.aaa, new SelectList(IEnumerable items, string dataValueField, string dataTextField, object selectedValue),是否水平显示)
标签:MVC
您可能感兴趣
- MVC中数据验证
- mysql数据结构讲解(详解MySQL 数据库隔离级别与MVCC)
- MVC中CheckBox
- mvc renderpartial
- mvc中@helper的用法
- MVC使用HandleErrorAttribute自定义异常
- MVC Model验证
- Asp.net Mvc模型绑定
- asp.net MVC处理流程
- mvc在视图中使用@helper封装输出代码
- mvc razor 输出html
- mvc中Scripts.Render、Styles.Render
- mysql乐观锁与mvcc(MySQL中的乐观锁,悲观锁和MVCC全面解析)
- MVC中的ActionResult
- php框架初始化教程学习(PHP从零开始打造自己的MVC框架之类的自动加载实现方法详解)
- iiS7中如何部署MVC3程序
- 爱情是什么(爱情是什么最经典的话)
- 乔欣 古装剧中的高颜值(古装剧中的高颜值)
- 怎么才可以财富自由(如何让自己实现财富自由)
- 为什么越来越多年轻人回农村(为什么越来越多年轻人回农村生活)
- 怎么快速学好英语(怎么快速学好英语初中)
- 中国留学生都是富二代吗()
热门推荐
- docker线上部署流程(Docker部署Rancher的方法无坑)
- dockerfile镜像介绍(使用Dockerfile构建docker镜像)
- python opencv 标记目标(使用Python的OpenCV模块识别滑动验证码的缺口推荐)
- laravel框架的运行机制(Laravel5框架自定义错误页面配置操作示例)
- mongodb简单介绍和安装
- django查找路径(Django Sitemap 站点地图的实现方法)
- idea里面怎么配置tomcat(intellij idea 使用Tomcat部署的项目位置在哪)
- javascript对象添加方法(详解JavaScript面向对象实战之封装拖拽对象)
- vue如何excel表格上传功能(Vue + iView实现Excel上传功能的完整代码)
- linux如何搭建ftp服务器(Linux下搭建ftp服务器)