HtmlHelper拓展实现RadioList

mvc中HtmlHelper可以帮助我们生成许多Html控件,但是没有类似DropDownList的RadioList,但是发现这些方法都是拓展方法,于是就想自己也拓展一个RadioList

从网上下载mvc源码中发现微软的方法还是很复杂的,添加了模型的校验

下面看一下我的吧

拓展方法

public static class CheeseBarExtend
{
    public static MvcHtmlString RadioList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList)
    {
        return RadioList(htmlHelper, name, selectList, null, null, null, 1);
    }
    public static MvcHtmlString RadioList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, int col)
    {
        return RadioList(htmlHelper, name, selectList, null, null, null, col);
    }
    public static MvcHtmlString RadioList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, string rowClass, string radioClass, string spanClass, int col)
    {
        return RadioListHelper(htmlHelper, metadata: null, name: name, selectList: selectList, rowClass: rowClass, radioClass: radioClass, spanClass: spanClass, col: col);
    }
    public static MvcHtmlString RadioListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList)
    {
        return RadioListFor(htmlHelper, expression, selectList, null, null, null, 1);
    }
    public static MvcHtmlString RadioListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, int col)
    {
        return RadioListFor(htmlHelper, expression, selectList, null, null, null, col);
    }
    public static MvcHtmlString RadioListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string rowClass, string radioClass, string spanClass, int col)
    {
        if (expression == null)
        {
            throw new ArgumentNullException("expression");
        }
        //用来模型验证
        ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
        return RadioListHelper(htmlHelper, metadata, ExpressionHelper.GetExpressionText(expression), selectList, rowClass, radioClass, spanClass, col);
    }
    public static MvcHtmlString RadioListHelper(HtmlHelper htmlHelper, ModelMetadata metadata, string name, IEnumerable<SelectListItem> selectList, string rowClass, string radioClass, string spanClass, int col)
    {
        StringBuilder resultString = new StringBuilder();
        if (radioClass == null) radioClass = "";
        if (rowClass == null) rowClass = "";
        if (spanClass == null) spanClass = "";
        StringBuilder radio = new StringBuilder("<input type=\"radio\" class=\"" + radioClass + "\" name=\"" + name + "\" value=\"noValue\" isChecked /><span class=\"" + spanClass + "\">noText</span>");
        StringBuilder tempRadio = new StringBuilder();
        StringBuilder tempLine = new StringBuilder();

        int tempCol = col;
        foreach (SelectListItem selectItem in selectList)
        {
            tempRadio = new StringBuilder(radio.ToString());
            if (selectItem.Selected)
            {
                tempRadio.Replace("isChecked", "checked");
            }
            else
            {
                tempRadio.Replace("isChecked", "");
            }
            tempRadio.Replace("noValue", selectItem.Value);
            tempRadio.Replace("noText", selectItem.Text);
            tempLine.Append(tempRadio);
            if (--tempCol == 0)
            {//要换行
                tempLine.WearDiv(rowClass);
                resultString.Append(tempLine);
                tempCol = col;
                tempLine.Clear();
            }
        }
        return new MvcHtmlString(resultString.ToString());
    }
    public static StringBuilder WearDiv(this StringBuilder sb, string className)
    {
        string divBeg = "<div class=\"" + className + "\">";
        string divEnd = "</div>";
        sb.Insert(0, divBeg);
        sb.Append(divEnd);
        return sb;
    }
}

 HttpGet

        public ActionResult EditPerson(string id)
        {
            IService service = new Service();
            var person = service.GetPersons().FirstOrDefault(lbItem => lbItem.Id == id);
            if (person == null)
                throw new NullReferenceException();
            //性别列表
            var sexList = new List<object>();
            sexList.Add(new { Value = "nan", Text = "男" });
            sexList.Add(new { Value = "nv", Text = "女" });
            var sexSelectList = new SelectList(sexList, "Value", "Text",person.Sex);
            //学位列表
            var dipList = new List<object>();
            dipList.Add(new { Value = "dz", Text = "大专" });
            dipList.Add(new { Value = "bs", Text = "博士" });
            dipList.Add(new { Value = "yjs", Text = "研究生" });
            dipList.Add(new { Value = "gz", Text = "高中" });
            var dipSelectList = new SelectList(dipList, "Value", "Text",person.Diploma);

            ViewData["RadioSexList"] = sexSelectList;
            ViewData["RadioDiplomaList"] = dipSelectList;

            return View(person);
        }

 cshtml

@model MyExtend.Controllers.Person
@{
    Layout = null;
    ViewBag.Title = "EditPerson";
}

<h2>EditPerson</h2>

@using (Html.BeginForm("SaveEdit", "CheeseBar", FormMethod.Post))
{
    <div>
        @Html.EditorFor(model => model.Name)
    </div>
    <div>
        @Html.RadioListFor(model => model.Sex, (SelectList)ViewData["RadioSexList"],"rowClass","radioClass","spanClass",1)
    </div>
    <div>
        @Html.RadioList("Diploma", (SelectList)ViewData["RadioDiplomaList"],"rowClass", "radioClass", "spanClass", 2)
    </div>
}

 

编辑后提交,HttpPost入口处添加断点

 

转载于:https://www.cnblogs.com/cheesebar/p/5678455.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值