学习MVC时候前端通常会用到HtmlHelper,使得前端编码简便很多。我们可能会经常用到htmlHelper中一些的EditorFor,LabelFor,ValiationMessageFor,
发现这些方法都是在System.Web.Mvc.Html 命名空间下定义的一些各类Extensions类。通过反编译 namespace System.Web.Mvc.Html
{
public static class HtmlExtension
{
/// <summary>
/// 扩展radiobutton 列表
/// </summary>
/// <typeparam name="TModel">实体</typeparam>
/// <typeparam name="TValue">属性</typeparam>
/// <param name="helper"></param>
/// <param name="expression">表达式</param>
/// <param name="items">数据列表</param>
/// <param name="column">每行显示个数</param>
/// <param name="attributes">html属性</param>
/// <returns></returns>
public static MvcHtmlString RadioButtonListFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, IEnumerable<SelectListItem> items, int column = , object attributes = null)
{
string raidobuttonStr = "";
BuildListTag(out raidobuttonStr, "radio", items, expression, column, attributes);
return MvcHtmlString.Create(raidobuttonStr);
} /// <summary>
/// 扩展radiobutton 列表
/// </summary>
/// <typeparam name="TModel">实体</typeparam>
/// <typeparam name="TValue">属性</typeparam>
/// <param name="helper"></param>
/// <param name="expression">表达式</param>
/// <param name="viewDataName">viewData数据列表名称</param>
/// <param name="column">每行显示个数</param>
/// <param name="attributes">属性</param>
/// <returns></returns>
public static MvcHtmlString RadioButtonListFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, string viewDataName, int column = , object attributes = null)
{
string raidobuttonStr = "";
var items = helper.ViewData[viewDataName] as List<SelectListItem>;
BuildListTag(out raidobuttonStr, "radio", items, expression, column, attributes);
return MvcHtmlString.Create(raidobuttonStr);
} /// <summary>
/// 扩展radiobutton 列表
/// </summary>
/// <typeparam name="TModel">实体</typeparam>
/// <typeparam name="TValue">属性</typeparam>
/// <param name="helper"></param>
/// <param name="expression">表达式</param>
/// <param name="items">数据列表</param>
/// <param name="column">每行显示个数</param>
/// <param name="attributes">html属性</param>
/// <returns></returns>
public static MvcHtmlString CheckBoxListFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, IEnumerable<SelectListItem> items, int column = , object attributes = null)
{
string raidobuttonStr = "";
BuildListTag(out raidobuttonStr, "checkbox", items, expression, column, attributes);
return MvcHtmlString.Create(raidobuttonStr);
} /// <summary>
/// 扩展radiobutton 列表
/// </summary>
/// <typeparam name="TModel">实体</typeparam>
/// <typeparam name="TValue">属性</typeparam>
/// <param name="helper"></param>
/// <param name="expression">表达式</param>
/// <param name="viewDataName">viewData数据列表名称</param>
/// <param name="column">每行显示个数</param>
/// <param name="attributes">属性</param>
/// <returns></returns>
public static MvcHtmlString CheckBoxListFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, string viewDataName, int column = , object attributes = null)
{
string raidobuttonStr = "";
var items = helper.ViewData[viewDataName] as List<SelectListItem>;
BuildListTag(out raidobuttonStr, "checkbox", items, expression, column, attributes);
return MvcHtmlString.Create(raidobuttonStr);
} /// <summary>
/// 构造radioList或者checkBoxList标签
/// </summary>
/// <typeparam name="TModel"></typeparam>
/// <typeparam name="TValue"></typeparam>
/// <param name="raidobuttonStr">拼接的字符窜</param>
/// <param name="tag">标签(checkbox or radio)</param>
/// <param name="expression">表达式</param>
/// <param name="items">数据列表</param>
/// <param name="column">每行显示个数</param>
/// <param name="attributes">属性</param>
private static void BuildListTag<TModel, TValue>(out string raidobuttonStr, string tag, IEnumerable<SelectListItem> items, Expression<Func<TModel, TValue>> expression, int column = , object attributes = null)
{
raidobuttonStr = "";
if (items != null && items.Any())
{
int count = ;
///获取表达式属性名称
var name = (expression.Body as MemberExpression).Member.Name;
foreach (var item in items)
{
TagBuilder raidobutton = new TagBuilder("input");
raidobutton.Attributes.Add("type", tag);
raidobutton.Attributes.Add("name", name);
raidobutton.Attributes.Add("value", item.Value);
if (item.Selected)
{
raidobutton.Attributes.Add("checked", "checked");
}
if (attributes != null)
{
raidobutton.MergeAttributes(new RouteValueDictionary(attributes));
} raidobuttonStr += raidobutton.ToString(TagRenderMode.SelfClosing);
raidobuttonStr += item.Text;
raidobuttonStr += "&nbsp;&nbsp;&nbsp;"; if (column == )
{
raidobuttonStr += "<br/>";
}
///根据每行显示个数设置换行
else
{
if (count == column && column != )
{
raidobuttonStr += "<br/>";
}
}
count++;
}
}
}
}
}
产品类型:@Html.CheckBoxListFor(m=>m.ProductTypeId, (SelectList)ViewData["ProductType"],3,null)  // 每行显示3个
产品类型:@Html.RadioButtonListFor(m => m.ProductTypeId, (SelectList)ViewData["ProductType"], 1, null)  每行显示1个

MVC 扩展RadioButtonListFor和CheckBoxListFor的更多相关文章

  1. 扩展RadioButtonListFor和CheckBoxListFor

    在我们做正常的MVC的开发中,一些基本的控件已经够用了,但是有时候我们需要用到库里面没有的一些控件,比如RadioButtonListFor和CheckBoxListFor这类的列表控件,在MVC库里 ...

  2. Asp.net 面向接口可扩展框架之“Mvc扩展框架及DI”

    标题“Mvc扩展框架及DI”有点绕口,我也想不出好的命名,因为这个内容很杂,涉及多个模块,但在日常开发又密不可分 首先说Mvc扩展框架,该Mvc扩展就是把以前的那个Mvc分区扩展框架迁移过来,并优化整 ...

  3. MVC 扩展 Html.ImageFor

    Asp.Net MVC 扩展 Html.ImageFor 方法详解 背景: 在Asp.net MVC中定义模型的时候,DataType有DataType.ImageUrl这个类型,但htmlhelpe ...

  4. 面向接口可扩展框架之“Mvc扩展框架及DI”

    面向接口可扩展框架之“Mvc扩展框架及DI” 标题“Mvc扩展框架及DI”有点绕口,我也想不出好的命名,因为这个内容很杂,涉及多个模块,但在日常开发又密不可分 首先说Mvc扩展框架,该Mvc扩展就是把 ...

  5. MVC扩展ModelBinder使类型为DateTime的Action参数可以接收日期格式的字符串

    原文:MVC扩展ModelBinder使类型为DateTime的Action参数可以接收日期格式的字符串 如何让视图通过某种途径,把符合日期格式的字符串放到路由中,再传递给类型为DateTime的控制 ...

  6. 前端基于easyui的mvc扩展(续)

    前端基于easyui的mvc扩展(续) 回顾及遗留问题 上一篇讲解了基于easyui的mvc扩展的基本实现,已经降低了在mvc内使用easyui的难度,但是仍然还有一些问题: 当我们要给生成的控件设置 ...

  7. ASP.NET MVC扩展库

    很多同学都读过这篇文章吧 ASP.NET MVC中你必须知道的13个扩展点,今天给大家介绍一个ASP.NET MVC的扩展库,主要就是针对这些扩展点进行.这个项目的核心是IOC容器,包括Ninject ...

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

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

  9. 17+个ASP.NET MVC扩展点【附源码】

    1.自定义一个HttpModule,并将其中的方法添加到HttpApplication相应的事件中!即:创建一个实现了IHttpmodule接口的类,并将配置WebConfig.  在自定义的Http ...

随机推荐

  1. hdu4614 Vases and Flowers【线段树】【二分】

    Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N ...

  2. 一次使用Python连接数据库生成二维码并安装为windows服务的工作任务

    最近有一个需求,在现有生产系统上的人员库中增加一个此人员关键信息的二维码,支持文字版和跳转版两种方式,与报表工具关联,可打印.以windows服务方式,定时检查,只要发现某人员没有此二维码信息,就生成 ...

  3. Chainer的初步学习

    人们都说Chainer是一块非常灵活you要用的框架,今天接着项目里面的应用,初步接触一下,涨涨姿势,直接上源码吧,看着好理解.其实跟Tensorflow等其他框架都是一个套路,个人感觉更简洁了. & ...

  4. 实现 TensorFlow 架构的规模性和灵活性

    TensorFlow https://mp.weixin.qq.com/s/tEyX596WXTzsABXaeTesug

  5. 3322.org

    3322.org — Krebs on Security https://krebsonsecurity.com/tag/3322-org/

  6. complexity_action

    大话数据结构 /* 顺序存储的结构 */ #define MAXSIZE 20 //存储空间初始分配量 typedef int ElemType; //ElemType类型根据实际情况而定,这里假设为 ...

  7. Linux目录【持续更新中】

    故障排除 服务器为什么这么慢?耗尽了CPU.RAM和磁盘I/O资源 服务 ELK服务基础 基础 常用命令 curl命令 Nginx服务基础 Nginx正向代理配置 Nginx文件下载服务器 Nginx ...

  8. Steady Cow Assignment---poj3189(多重匹配+二分)

    题目链接:http://poj.org/problem?id=3189 题意:有n头牛,B个牛棚,每头牛对牛棚都有一个喜欢度,接下来输入N*B的矩阵第i行第j列的数x表示:第i头牛第j喜欢的是x; 第 ...

  9. Catch That Cow--POJ3278

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  10. 对innodb_flush_log_at_commit参数的写日志和刷盘行为进行图解

    对innodb_flush_log_at_commit参数的写日志和刷盘行为进行图解