asp.net mvc中构建htmlHelper的checkboxlist

来源:互联网 发布:长沙cnc编程招聘信息 编辑:程序博客网 时间:2024/05/28 23:19

新建一个文件夹,命名为Extensions,新建ListControlExtensions.cs文件,拷贝下面的代码。文件夹和文件叫什么名字,放哪里无所谓,上面只是个人习惯而已。

想必聪明如你,下面的代码读起来也没什么难度,反正就是给HtmlHelper添加了几个扩展方法,为了让其跟mvc原生提供的方法用起来差不多,多提供了集中参数组合,并且添加了CheckBoxFor的方法。

using System;using System.Collections.Generic;using System.Linq.Expressions;using System.Runtime.CompilerServices;using System.Text;using System.Web.Mvc;using System.Web.Routing;namespace System.Web.Mvc{    public static class ListControlExtensions    {        public static MvcHtmlString CheckBoxList(this HtmlHelper htmlHelper, string name)        {            return CheckBoxList(htmlHelper, name, null, "", null);        }        public static MvcHtmlString CheckBoxList(this HtmlHelper htmlHelper, string name, string value)        {            return CheckBoxList(htmlHelper, name, null, value, null);        }        public static MvcHtmlString CheckBoxList(this HtmlHelper htmlHelper, string name, object htmlAttributs)        {            return CheckBoxList(htmlHelper, name, null, "", htmlAttributs);        }        public static MvcHtmlString CheckBoxList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, object htmlAttributes)        {            return CheckBoxList(htmlHelper, name, selectList, "", htmlAttributes);        }        public static MvcHtmlString CheckBoxList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, string value, object htmlAttributes)        {            IEnumerable<SelectListItem> source = null;            if (selectList != null)            {                source = selectList;            }            else            {                source = (IEnumerable<SelectListItem>)htmlHelper.ViewData[name];            }            return RenderToHtml(name, source, "checkbox", htmlAttributes, value);        }        public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList)        {            return CheckBoxListFor<TModel, TProperty>(htmlHelper, expression, selectList, null);        }        public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, object htmlAttributes)        {            ModelMetadata data = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);            return RenderToHtml(data.PropertyName, selectList, "checkbox", htmlAttributes, data.Model);        }        private static MvcHtmlString RenderToHtml(string name, IEnumerable<SelectListItem> source, string type, object htmlAttributes, object value)        {            int index = 1;            StringBuilder sb = new StringBuilder();            if (source == null)            {                return new MvcHtmlString("");            }            string[] vals;            if (value == null)            {                vals = new string[] { };            }            else            {                vals = (string[])value;            }            string attr = "";            var dict = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);            foreach (var d in dict)            {                if (d.Value != null)                {                    attr += " " + d.Key + "=\"" + d.Value.ToString() + "\"";                }            }            foreach (var s in source)            {                string n = name + index.ToString();                //if (htmlAttributes != null)                //{                //    tb.MergeAttributes(new RouteValueDictionary(htmlAttributes));                //}                sb.Append("<label for=\"" + n + "\"" + " ");                if (attr.Length > 0)                {                    sb.Append(attr);                }                sb.Append(">");                sb.Append("<input name=\"" + name + "\" id=\"" + n + "\"" + " type=\"" + type + "\" value=\"" + s.Value + "\"");                if (s.Selected || Array.IndexOf(vals, s.Value) > -1)                {                    sb.Append(" checked=\"checked\"");                }                sb.Append(" />" + s.Text);                sb.AppendLine("</label>");                index++;            }            return new MvcHtmlString(sb.ToString());        }    }}

好了,不废话,文件保存好之后,就可以在cshtml里调用了,这里给一个例子:

@Html.CheckBoxListFor(model => model.Category, ViewBag.Cates as IEnumerable<SelectListItem>, new { @class = "checkbox-inline" })

搞定,跟定义其他原生标签没什么不同!

这里只实现了CheckBoxList,其他类似的直接复制几个即可。忘了为啥要写这个了,似乎是为了可以多选????好像是这么回事儿!!

0 0
原创粉丝点击