经过一番折腾(主要是SelectList这个类操作有些繁琐)实现了CheckBoxList,过程RadioList基本一样

拓展方法

    public static MvcHtmlString CheckBoxList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList)
{
return CheckBoxList(htmlHelper, name, selectList, null, null, null, 1);
}
public static MvcHtmlString CheckBoxList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, int col)
{
return CheckBoxList(htmlHelper, name, selectList, null, null, null, col);
}
public static MvcHtmlString CheckBoxList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, string rowClass, string CheckBoxClass, string spanClass, int col)
{
return CheckBoxListHelper(htmlHelper, metadata: null, name: name, selectList: selectList, rowClass: rowClass, checkBoxClass: CheckBoxClass, spanClass: spanClass, col: col);
}
public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList)
{
return CheckBoxListFor(htmlHelper, expression, selectList, null, null, null, 1);
}
public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, int col)
{
return CheckBoxListFor(htmlHelper, expression, selectList, null, null, null, col);
}
public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string rowClass, string CheckBoxClass, string spanClass, int col)
{
if (expression == null)
{
throw new ArgumentNullException("expression");
}
//可以不用
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
return CheckBoxListHelper(htmlHelper, metadata, ExpressionHelper.GetExpressionText(expression), selectList, rowClass, CheckBoxClass, spanClass, col);
}
public static MvcHtmlString CheckBoxListHelper(this HtmlHelper htmlHelper, ModelMetadata metadata, string name, IEnumerable<SelectListItem> selectList, string rowClass, string checkBoxClass, string spanClass, int col)
{
StringBuilder resultString = new StringBuilder();
if (checkBoxClass == null) checkBoxClass = "";
if (rowClass == null) rowClass = "";
if (spanClass == null) spanClass = "";
StringBuilder checkBox = new StringBuilder("<input type=\"checkBox\" class=\"" + checkBoxClass + "\" name=\"" + name + "\" value=\"noValue\" isChecked /><span class=\"" + spanClass + "\">noText</span>");
StringBuilder tempcheckBox = new StringBuilder();
StringBuilder tempLine = new StringBuilder();
var selectValues = (IEnumerable<string>)((SelectList)selectList).SelectedValue; int tempCol = col;
foreach (SelectListItem selectItem in selectList)
{
tempcheckBox = new StringBuilder(checkBox.ToString());
if (selectValues.Contains(selectItem.Value))
{
tempcheckBox.Replace("isChecked", "checked");
}
else
{
tempcheckBox.Replace("isChecked", "");
}
tempcheckBox.Replace("noValue", selectItem.Value);
tempcheckBox.Replace("noText", selectItem.Text);
tempLine.Append(tempcheckBox);
if (--tempCol == 0)
{//要换行
tempLine.WearDiv(rowClass);
resultString.Append(tempLine);
tempCol = col;
tempLine.Clear();
}
}
if (tempLine.Length != 0)
{
tempLine.WearDiv("");
}
resultString.Append(tempLine);
return new MvcHtmlString(resultString.ToString());
}

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);
//爱好列表
var personHobbies = person.Hobbies.ToList();
var allHobbies = service.GetHobbies().ToList();
var hobbySelectList = new SelectList(allHobbies, "Id", "Name", personHobbies.Select(a => a.Id).ToList()); ViewData["RadioSexList"] = sexSelectList;
ViewData["RadioDiplomaList"] = dipSelectList;
ViewData["CheckBoxHobbyList"] = hobbySelectList;
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> <div>
@Html.CheckBoxListFor(model => model.Hobbies, (SelectList)ViewData["CheckBoxHobbyList"],2)
</div>
}

编辑后提交,save方法添加断点

HtmlHelper拓展实现CheckBoxList的更多相关文章

  1. HtmlHelper拓展实现RadioList

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

  2. .NET MVC3中扩展一个HtmlHelper方法CheckBoxList

    MVC中有DropDownList方法,挺好用,可是最常用的需求,一组checkboxlist咋没个类似方法呢?郁闷之余,自己做一个吧,直接上代码 public static MvcHtmlStrin ...

  3. MVC扩展HtmlHelper,加入RadioButtonList、CheckBoxList、DropdownList

    代码: using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions ...

  4. MVC CheckBoxList的实现

    using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; us ...

  5. ASP.NET MVC 3 CheckBoxList 的使用

    在以前的 ASP.NET MVC 中可以直接使用 CheckBoxList,但后来不知道什么原因在 MVC 中移除了 CheckBoxList,所以默认情况下 ASP.NET MVC 3 中是没有 C ...

  6. MVC生成CheckBoxList并对其验证

    原文:MVC生成CheckBoxList并对其验证 通过扩展方法,可以让CheckBox水平排列,生成CheckBoxList,正如"MVC扩展生成CheckBoxList并水平排列&quo ...

  7. ASP .NET MVC HtmlHelper扩展——简化“列表控件”的绑定

    在众多表单元素中,有一类<select>元素用于绑定一组预定义列表.传统的ASP.NET Web Form中,它对应着一组重要的控件类型,即ListControl,我们经常用到DropDo ...

  8. 再议ASP.NET MVC中CheckBoxList的验证

    在ASP.NET MVC 4中谈到CheckBoxList,经常是与CheckBoxList的显示以及验证有关.我在"MVC扩展生成CheckBoxList并水平排列"中通过扩展H ...

  9. MVC扩展生成CheckBoxList并水平排列

    本篇体验生成CheckBoxList的几个思路,扩展MVC的HtmlHelper生成CheckBoxList,并使之水平排开.     通过遍历从控制器方法拿到的Model集合 □ 思路 比如为一个用 ...

随机推荐

  1. International Conference for Smart Health 2015 Call for Papers

    Advancing Informatics for healthcare and healthcare applications has become an international researc ...

  2. Webpack教程

    打开链接:Webpack教程

  3. IDisposable的另类用法

    IDisposable是.Net中一个很重要的接口,一般用来释放非托管资源,我们知道在使用了IDisposable的对象之后一定要调用IDisposable.Dispose()方法,或者使用.Net提 ...

  4. Qcon会议之所见所想

    作为普通码农一枚,Qcon是俺参与过的最高级的技术大会了.大会共历时三天,因为俺第二天就得赶火车休个五一大长假,所以只参加了第一天4/25号的会议(其他俩天自然有其他同事会去观摩),不过第一天的会议有 ...

  5. [译]libev和libevent的设计差异

    本文译自what's the difference between libev and libevent? 作者是libev作者 [问]两个库都是为异步io调度而设计,在Linux上都是使用epoll ...

  6. [Canvas前端游戏开发]——FlappyBird详解

    一直想自己做点小东西,直到最近看了本<HTML5游戏开发>,才了解游戏开发中的一点点入门知识. 本篇就针对学习的几个样例,自己动手实践,做了个FlappyBird,源码共享在度盘 :也可以 ...

  7. Intellij IDEA 13.1.3 打开多个窗口项目

    作者QQ:1095737364 Intellij 是Java开发人员最喜欢的编程工具.默认情况只能打开一个窗口一个项目,本文就是教大家打开多个窗口,实现同时打开多个项目 第一步:打开Intellij, ...

  8. C#设计模式-桥接模式

    这里以电视遥控器的一个例子来引出桥接模式解决的问题,首先,我们每个牌子的电视机都有一个遥控器,此时我们能想到的一个设计是——把遥控器做为一个抽象类,抽象类中提供遥控器的所有实现,其他具体电视品牌的遥控 ...

  9. 设计模式之里氏代换原则(LSP)

    里氏代换原则(Liskov Substitution Principle, LSP) 1 什么是里氏代换原则 里氏代换原则是由麻省理工学院(MIT)计算机科学实验室的Liskov女士,在1987年的O ...

  10. Visulalization Voronoi in OpenSceneGraph

    Visulalization Voronoi in OpenSceneGraph eryar@163.com Abstract. In mathematics a Voronoi diagram is ...