HtmlHelper拓展实现CheckBoxList
经过一番折腾(主要是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的更多相关文章
- HtmlHelper拓展实现RadioList
mvc中HtmlHelper可以帮助我们生成许多Html控件,但是没有类似DropDownList的RadioList,但是发现这些方法都是拓展方法,于是就想自己也拓展一个RadioList 从网上下 ...
- .NET MVC3中扩展一个HtmlHelper方法CheckBoxList
MVC中有DropDownList方法,挺好用,可是最常用的需求,一组checkboxlist咋没个类似方法呢?郁闷之余,自己做一个吧,直接上代码 public static MvcHtmlStrin ...
- MVC扩展HtmlHelper,加入RadioButtonList、CheckBoxList、DropdownList
代码: using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions ...
- MVC CheckBoxList的实现
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; us ...
- ASP.NET MVC 3 CheckBoxList 的使用
在以前的 ASP.NET MVC 中可以直接使用 CheckBoxList,但后来不知道什么原因在 MVC 中移除了 CheckBoxList,所以默认情况下 ASP.NET MVC 3 中是没有 C ...
- MVC生成CheckBoxList并对其验证
原文:MVC生成CheckBoxList并对其验证 通过扩展方法,可以让CheckBox水平排列,生成CheckBoxList,正如"MVC扩展生成CheckBoxList并水平排列&quo ...
- ASP .NET MVC HtmlHelper扩展——简化“列表控件”的绑定
在众多表单元素中,有一类<select>元素用于绑定一组预定义列表.传统的ASP.NET Web Form中,它对应着一组重要的控件类型,即ListControl,我们经常用到DropDo ...
- 再议ASP.NET MVC中CheckBoxList的验证
在ASP.NET MVC 4中谈到CheckBoxList,经常是与CheckBoxList的显示以及验证有关.我在"MVC扩展生成CheckBoxList并水平排列"中通过扩展H ...
- MVC扩展生成CheckBoxList并水平排列
本篇体验生成CheckBoxList的几个思路,扩展MVC的HtmlHelper生成CheckBoxList,并使之水平排开. 通过遍历从控制器方法拿到的Model集合 □ 思路 比如为一个用 ...
随机推荐
- C#集合类型大盘点
C#集体类型( Collections in C#) 集合是.NET FCL(Framework Class Library)中很重要的一部分,也是我们开发当中最常用到的功能之一,几乎是无处不在.俗话 ...
- Hadoop学习笔记—20.网站日志分析项目案例(三)统计分析
网站日志分析项目案例(一)项目介绍:http://www.cnblogs.com/edisonchou/p/4449082.html 网站日志分析项目案例(二)数据清洗:http://www.cnbl ...
- Asp.Net MVC 分页、检索、排序整体实现
很多时候需要这样的功能,对表格进行分页.排序和检索.这个有很多实现的方式,有现成的表格控件.用前端的mvvm,用户控件.但很多时候看着很漂亮的东西你想进一步控制的时候却不那么如意.这里自己实现一次,功 ...
- 备忘-Sql server Timeout expired 超时时间已到. 达到了最大池大小 错误及Max Pool Size设置
select * from sysprocesses where dbid= db_id('数据库名') 通过此语句可查看目前所有的连接进程 不够了就必须设置Max Pool Size,理论最大值为3 ...
- 介绍两个Ubuntu上的桌面小工具
经常使用Windows10,Sticky Notes和壁纸自动切换功能挺好用的.我经常会使用Sticky Notes来记录一些信息,内容是实时保存的,而且启动的时候会自动显示在桌面上.其实Ubuntu ...
- 海淘手表Invicta8926OB到手~晒图
3月3号通过国内代购网站Hai360海外购下单: 3月5号美亚发货: 3月6号到达转运仓: 3月12号到达天津清关: 清关等了7天: 3月19号转国内快递,我将原武汉地址,改上海,耽误了3天: 3月2 ...
- [python] 安装numpy+scipy+matlotlib+scikit-learn及问题解决
这篇文章主要讲述Python如何安装Numpy.Scipy.Matlotlib.Scikit-learn等库的过程及遇到的问题解决方法.最近安装这个真是一把泪啊,各种不兼容问题和报错,希望文章对你有所 ...
- JS中script词法分析
核心:JS中的script是分段执行的. <script> var i = 10; </script> <script> alert(i); </script ...
- [转] Android优秀开源项目
Android经典的开源项目其实非常多,但是国内的博客总是拿着N年前的一篇复制来复制去,实在是不利于新手学习.今天爬爬把自己熟悉的一些开源项目整理起来,希望能对Android开发同学们有所帮助.另外, ...
- Ajax_03之接收数据
1.使用XHR接收服务器返回的数据--text 服务器端: header('Content-Type:text/plain'); echo 'xxx'; 客户端: xhr.responseTex ...