标签:
在我们做正常的MVC的开发中,一些基本的控件已经够用了,但是有时候我们需要用到库里面没有的一些控件,比如RadioButtonListFor和CheckBoxListFor这类的列表控件,在MVC库里面没提供,需要自己来扩展一下。我们通过MvcHtmlString扩展的控件,最终还是被转换为html标签的形式,所以扩展控件实质上还是拼标签。其中用到TagBuilder这个类,是mvc自带的生成标签字符窜的类,大家没事的时候搜一下看看具体用法。还有selectListItem这个类,主要包含键值属性和是否选中的属性 。以下代码只是根据个人思路来做的,仅供参考。
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Web; using System.Web.Routing; using System.Web.UI; namespace System.Web.Mvc.Html { public static class HtmlExtension { /// <summary> /// 扩展radiobutton 列表 /// </summary> /// <typeparam name="TModel">实体</typeparam> /// <typeparam name="TValue">属性</typeparam> /// <param name="helper"></param> /// <param name="expression">表达式</param> /// <param name="items">数据列表</param> /// <param name="column">每行显示个数</param> /// <param name="attributes">html属性</param> /// <returns></returns> public static MvcHtmlString RadioButtonListFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, IEnumerable<SelectListItem> items, int column = 0, object attributes = null) { string raidobuttonStr = ""; BuildListTag(out raidobuttonStr, "radio", items, expression, column, attributes); return MvcHtmlString.Create(raidobuttonStr); } /// <summary> /// 扩展radiobutton 列表 /// </summary> /// <typeparam name="TModel">实体</typeparam> /// <typeparam name="TValue">属性</typeparam> /// <param name="helper"></param> /// <param name="expression">表达式</param> /// <param name="viewDataName">viewData数据列表名称</param> /// <param name="column">每行显示个数</param> /// <param name="attributes">属性</param> /// <returns></returns> public static MvcHtmlString RadioButtonListFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, string viewDataName, int column = 0, object attributes = null) { string raidobuttonStr = ""; var items = helper.ViewData[viewDataName] as List<SelectListItem>; BuildListTag(out raidobuttonStr, "radio", items, expression, column, attributes); return MvcHtmlString.Create(raidobuttonStr); } /// <summary> /// 扩展radiobutton 列表 /// </summary> /// <typeparam name="TModel">实体</typeparam> /// <typeparam name="TValue">属性</typeparam> /// <param name="helper"></param> /// <param name="expression">表达式</param> /// <param name="items">数据列表</param> /// <param name="column">每行显示个数</param> /// <param name="attributes">html属性</param> /// <returns></returns> public static MvcHtmlString CheckBoxListFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, IEnumerable<SelectListItem> items, int column = 0, object attributes = null) { string raidobuttonStr = ""; BuildListTag(out raidobuttonStr, "checkbox", items, expression, column, attributes); return MvcHtmlString.Create(raidobuttonStr); } /// <summary> /// 扩展radiobutton 列表 /// </summary> /// <typeparam name="TModel">实体</typeparam> /// <typeparam name="TValue">属性</typeparam> /// <param name="helper"></param> /// <param name="expression">表达式</param> /// <param name="viewDataName">viewData数据列表名称</param> /// <param name="column">每行显示个数</param> /// <param name="attributes">属性</param> /// <returns></returns> public static MvcHtmlString CheckBoxListFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, string viewDataName, int column = 0, object attributes = null) { string raidobuttonStr = ""; var items = helper.ViewData[viewDataName] as List<SelectListItem>; BuildListTag(out raidobuttonStr, "checkbox", items, expression, column, attributes); return MvcHtmlString.Create(raidobuttonStr); } /// <summary> /// 构造radioList或者checkBoxList标签 /// </summary> /// <typeparam name="TModel"></typeparam> /// <typeparam name="TValue"></typeparam> /// <param name="raidobuttonStr">拼接的字符窜</param> /// <param name="tag">标签(checkbox or radio)</param> /// <param name="expression">表达式</param> /// <param name="items">数据列表</param> /// <param name="column">每行显示个数</param> /// <param name="attributes">属性</param> private static void BuildListTag<TModel, TValue>(out string raidobuttonStr, string tag, IEnumerable<SelectListItem> items, Expression<Func<TModel, TValue>> expression, int column = 0, object attributes = null) { raidobuttonStr = ""; if (items != null && items.Any()) { int count = 1; ///获取表达式属性名称 var name = (expression.Body as MemberExpression).Member.Name; foreach (var item in items) { TagBuilder raidobutton = new TagBuilder("input"); raidobutton.Attributes.Add("type", tag); raidobutton.Attributes.Add("name", name); raidobutton.Attributes.Add("value", item.Value); if (item.Selected) { raidobutton.Attributes.Add("checked", "checked"); } if (attributes != null) { raidobutton.MergeAttributes(new RouteValueDictionary(attributes)); } raidobuttonStr += raidobutton.ToString(TagRenderMode.SelfClosing); raidobuttonStr += item.Text; raidobuttonStr += " "; if (column == 1) { raidobuttonStr += "<br/>"; } ///根据每行显示个数设置换行 else { if (count == column && column != 0) { raidobuttonStr += "<br/>"; } } count++; } } } } }
下面是调用的方法
控制器里面代码:
var list = new List<SelectListItem>(); list.Add(new SelectListItem() { Text = "aaa", Value = "1", Selected = false }); list.Add(new SelectListItem() { Text = "bbb", Value = "2", Selected = false }); list.Add(new SelectListItem() { Text = "ccc", Value = "3", Selected = false }); list.Add(new SelectListItem() { Text = "ddd", Value = "4", Selected = true }); ViewBag.list = list;
页面上的代码(两种方式):
(1)
@{ var list = ViewBag.list as List<SelectListItem>; } @Html.CheckBoxListFor(p=>p.AccountCode,list,3)//@Html.RadioButtonListFor(p=>p.AccountCode,list,3)
(2)
@Html.CheckBoxListFor(p=>p.AccountCode,"list",3)//@Html.RadioButtonListFor(p=>p.AccountCode,"list",3)
其中的3代表每行显示3个,可以改为其他值,默认是显示一行
大致的代码就这么多,有不懂的或者有更好想法的,可以把代码贴在回复下面。
扩展RadioButtonListFor和CheckBoxListFor
标签:
原文地址:http://www.cnblogs.com/a546558309/p/4554592.html