asp.net mvc中构建htmlHelper的checkboxlist

新建一个文件夹,命名为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,其他类似的直接复制几个即可。忘了为啥要写这个了,似乎是为了可以多选????好像是这么回事儿!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值