经过一番折腾(主要是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. 协议分析 - DHCP协议解码详解

    协议分析 - DHCP协议解码详解 [DHCP协议简介]         DHCP,全称是 Dynamic Host Configuration Protocol﹐中文名为动态主机配置协议,它的前身是 ...

  2. Mono 3.0.12 支持可移植类库

    Mono 3.0.12已于6月19日发布.对跨平台开发者而言,对可移植类库的支持可能是该版本最重要的变化.该技术可以使一个DLL支持.NET.Windows Store.Windows Phone.S ...

  3. 剑指Offer面试题:26.字符串的排列

    一.题目:字符串的排列 题目:输入一个字符串,打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a.b.c所能排列出来的所有字符串abc.acb.bac.bca.cab和cba. 二 ...

  4. [每日电路图] 9、介绍一种低价简单的4.2V锂电池充电及稳压(LDO)电路——RT9193,SD8017

    消费类电子电路图设计中往往会涉及到4.2V锂离子电池充电电路.及4.2V转3.3V的LDO稳压电路,这里分享一种简单.便宜的设计方案: RT9193 300mA,Ultra-Low Noise,Ult ...

  5. 将nuget与VS直接集成,实现一键上传等功能

    nuget是一个非常方便的包管理工具,很多团队为了开发的方便也建立了自己的包源网站(nuget.server),本篇文章是笔者在配置nuget上面的一点小体,其最终目标是要达到能够在VS里一键打包上传 ...

  6. zookeeper分布式锁原理

    一.分布式锁介绍分布式锁主要用于在分布式环境中保护跨进程.跨主机.跨网络的共享资源实现互斥访问,以达到保证数据的一致性. 二.架构介绍在介绍使用Zookeeper实现分布式锁之前,首先看当前的系统架构 ...

  7. 轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)

    轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)(国家级奖项获奖作品升级版,四版累计印刷27次发行量超10万册的轻量级Jav ...

  8. distribution 中一直在运行 waitfor delay @strdelaytime 语句

    Replication 自动创建来一个 Job:Replication monitoring refresher for distribution,这个Agent执行一个sp: dbo.sp_repl ...

  9. SSIS Design2:增量更新

    一般来说,ETL实现增量更新的方式有两种,第一种:记录字段的最大值,如果数据源中存在持续增加的数据列,记录上次处理的数据集中,该列的最大值:第二种是,保存HashValue,快速检查所有数据,发现异动 ...

  10. 【Win10 应用开发】自适应Toast通知的XML文档结构

    老规矩,在开始之前老周先讲个故事. 话说公元2015年7月20日,VS 2015发布.于是,肯定有人会问老周了,C#6有啥新特性,我学不来啊.学不来的话你应该检讨.老周比较保守地计算一下,学会C# 6 ...