代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.UI; namespace System.Web.Mvc.Html
{
public static class HtmlExtension
{
/// <summary>
/// 扩展 DropdownList 列表1
/// </summary>
/// <param name="helper">扩展源</param>
/// <param name="name">元素名称</param>
/// <param name="items">SelectListItem集合</param>
/// <param name="attributes">html属性</param>
/// <returns></returns>
public static MvcHtmlString DropdownList(this HtmlHelper helper, string name, IEnumerable<SelectListItem> items, object attributes = null)
{
return helper.DropDownList(name, items, attributes);
}
/// <summary>
/// 扩展 DropdownList 列表2
/// </summary>
/// <param name="helper">扩展源</param>
/// <param name="name">元素名称</param>
/// <param name="viewDataName">ViewData键名称</param>
/// <param name="attributes">html属性</param>
/// <returns></returns>
public static MvcHtmlString DropdownList(this HtmlHelper helper, string name, string viewDataName, object attributes = null)
{
List<SelectListItem> list = helper.ViewData[viewDataName] as List<SelectListItem>;
return helper.DropDownList(name, list, attributes);
}
/// <summary>
/// 扩展 DropdownList 列表3
/// </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 DropdownListFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, IEnumerable<SelectListItem> items, object attributes = null)
{
return helper.DropDownListFor(expression, items, attributes);
} /// <summary>
/// 扩展 DropdownList 列表4
/// </summary>
/// <typeparam name="TModel">实体</typeparam>
/// <typeparam name="TValue">属性</typeparam>
/// <param name="helper"></param>
/// <param name="expression">表达式</param>
/// <param name="viewDataName">viewData数据列表名称</param>
/// <param name="attributes">html属性</param>
/// <returns></returns>
public static MvcHtmlString DropdownListFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, string viewDataName, object attributes = null)
{
List<SelectListItem> list = helper.ViewData[viewDataName] as List<SelectListItem>;
return helper.DropdownListFor(expression, list, attributes);
} /// <summary>
/// 扩展 radiobutton 列表1
/// </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 列表2
/// </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>
/// 扩展 CheckBox 列表1
/// </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>
/// 扩展 CheckBox 列表2
/// </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(item => item.AllRoles, "Roles", 5)
@Html.DropdownListFor(item => item.AllRoles, "Roles")
@Html.DropdownList("AllRoles", "Roles")
@Html.DropdownListFor(item => item.TypeID, ViewData["ArticleTypes"] as List<SelectListItem>)
还有其他的一些重载方法,我没有逐个写出,大家都可以试试。
http://www.cnblogs.com/artech/archive/2012/03/13/code-binding.html
http://www.cnblogs.com/a546558309/p/4554592.html

MVC扩展HtmlHelper,加入RadioButtonList、CheckBoxList、DropdownList的更多相关文章

  1. ASP.NET MVC 扩展HtmlHelper类方法

    1.扩展HtmlHelper类方法ShowPageNavigate 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...

  2. mvc 扩展htmlhelper

    using System.Web.Mvc; namespace System.Web.Mvc{    public static class HtmlExtend    {        public ...

  3. ASP.NET MVC 扩展HtmlHelper类为 js ,css 资源文件添加版本号

    写在前面 在项目部署当中会需要更新 css 文件或 js 等资源文件,为了避免由于浏览器缓存的原因无法加载新的 css 或 js ,一般的做法是在资源文件的后面加上一个版本号来解决,这样浏览器就会去服 ...

  4. mvc扩展HtmlHelper功能

    HtmlHelper详细介绍 简单示例 自定义HtmlHelper 解决: 直接写HTML的话如果语句有语法错误,如缺少结尾标记</b>,编译器不会报错,出来的页面可能会很乱且难以查出错误 ...

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

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

  6. MVC 自定义Htmlhelper扩展

    在MVC中,我们不仅可以使用它原来的方法,我们还可以自定义,这不不仅加大了我们开发的效率,同时使界面更简洁. 具体什么是扩展方法,你可以这样理解,必须是静态且在形参中第一个参数是以this开头,大概先 ...

  7. Asp.Net MVC 扩展 Html.ImageFor 方法详解

    背景: 在Asp.net MVC中定义模型的时候,DataType有DataType.ImageUrl这个类型,但htmlhelper却无法输出一个img,当用脚手架自动生成一些form或表格的时候, ...

  8. MVC中HtmlHelper用法大全参考

    MVC中HtmlHelper用法大全参考 解析MVC中HtmlHelper控件7个大类中各个控件的主要使用方法(1) 2012-02-27 16:25 HtmlHelper类在命令System.Web ...

  9. MVC 扩展 Html.ImageFor

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

随机推荐

  1. python爬虫小试

  2. express有中间件的增删改查

    var express = require('express');引入express框架 var router = express.Router();引入router路由级中间件 var data = ...

  3. CSS滚动条样式设置

    webkit浏览器css设置滚动条 主要有下面7个属性 ::-webkit-scrollbar 滚动条整体部分,可以设置宽度啥的 ::-webkit-scrollbar-button 滚动条两端的按钮 ...

  4. Kafka 基本原理

    Kafka 基本原理   来源:阿凡卢 , www.cnblogs.com/luxiaoxun/p/5492646.html 简介 Apache Kafka是分布式发布-订阅消息系统.它最初由Link ...

  5. flask中的蓝图与红图

    内容: 1.flask中的蓝图 2.flask子域名实现 3.flask中的红图 1.flask中的蓝图 一个大型项目中视图比较多,如果仅仅是写在app.py中不方便管理,蓝图就可以做到分功能分目录结 ...

  6. SQLserver查看索引使用情况

    查索引使用情况:    https://www.cnblogs.com/sunliyuan/p/6559354.html select db_name(database_id) as N'TOPK_T ...

  7. XE 创建 Active Form

    XE6: http://docwiki.embarcadero.com/RADStudio/XE6/en/Generating_an_Active_Form_Based_on_a_VCL_Form h ...

  8. 在linux中运行main方法所在的java类(亲测有效!!!)

    本人是用SecureCRTPortable连接linux终端的.其他工具连接linux终端应该是一样的操作! 一.首先到移动到java工程所在的bin目录. 二.在bin目录下执行javac -cp ...

  9. OpenCv 人脸识别 基础

    #include <opencv2\opencv.hpp> #include <iostream> using namespace std; int main() { // 摄 ...

  10. js中函数传参的情况

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...