在我们做正常的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 {////// 扩展radiobutton 列表 /// ///实体 ///属性 /// /// 表达式 /// 数据列表 /// 每行显示个数 /// html属性 ///public static MvcHtmlString RadioButtonListFor (this HtmlHelper helper, Expression > expression, IEnumerable items, int column = 0, object attributes = null) { string raidobuttonStr = ""; BuildListTag(out raidobuttonStr, "radio", items, expression, column, attributes); return MvcHtmlString.Create(raidobuttonStr); } /// /// 扩展radiobutton 列表 /// ///实体 ///属性 /// /// 表达式 /// viewData数据列表名称 /// 每行显示个数 /// 属性 ///public static MvcHtmlString RadioButtonListFor (this HtmlHelper helper, Expression > expression, string viewDataName, int column = 0, object attributes = null) { string raidobuttonStr = ""; var items = helper.ViewData[viewDataName] as List ; BuildListTag(out raidobuttonStr, "radio", items, expression, column, attributes); return MvcHtmlString.Create(raidobuttonStr); } /// /// 扩展radiobutton 列表 /// ///实体 ///属性 /// /// 表达式 /// 数据列表 /// 每行显示个数 /// html属性 ///public static MvcHtmlString CheckBoxListFor (this HtmlHelper helper, Expression > expression, IEnumerable items, int column = 0, object attributes = null) { string raidobuttonStr = ""; BuildListTag(out raidobuttonStr, "checkbox", items, expression, column, attributes); return MvcHtmlString.Create(raidobuttonStr); } /// /// 扩展radiobutton 列表 /// ///实体 ///属性 /// /// 表达式 /// viewData数据列表名称 /// 每行显示个数 /// 属性 ///public static MvcHtmlString CheckBoxListFor (this HtmlHelper helper, Expression > expression, string viewDataName, int column = 0, object attributes = null) { string raidobuttonStr = ""; var items = helper.ViewData[viewDataName] as List ; BuildListTag(out raidobuttonStr, "checkbox", items, expression, column, attributes); return MvcHtmlString.Create(raidobuttonStr); } /// /// 构造radioList或者checkBoxList标签 /// ////// /// 拼接的字符窜 /// 标签(checkbox or radio) /// 表达式 /// 数据列表 /// 每行显示个数 /// 属性 private static void BuildListTag (out string raidobuttonStr, string tag, IEnumerable items, Expression > 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 += " "; } ///根据每行显示个数设置换行 else { if (count == column && column != 0) { raidobuttonStr += " "; } } count++; } } } }}
下面是调用的方法
控制器里面代码:
var list = new List(); 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; }@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个,可以改为其他值,默认是显示一行
大致的代码就这么多,有不懂的或者有更好想法的,可以把代码贴在回复下面。