MVC扩展HtmlHelper,加入RadioButtonList、CheckBoxList、DropdownList
代码:
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 += " "; 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的更多相关文章
- 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 ...
- mvc 扩展htmlhelper
using System.Web.Mvc; namespace System.Web.Mvc{ public static class HtmlExtend { public ...
- ASP.NET MVC 扩展HtmlHelper类为 js ,css 资源文件添加版本号
写在前面 在项目部署当中会需要更新 css 文件或 js 等资源文件,为了避免由于浏览器缓存的原因无法加载新的 css 或 js ,一般的做法是在资源文件的后面加上一个版本号来解决,这样浏览器就会去服 ...
- mvc扩展HtmlHelper功能
HtmlHelper详细介绍 简单示例 自定义HtmlHelper 解决: 直接写HTML的话如果语句有语法错误,如缺少结尾标记</b>,编译器不会报错,出来的页面可能会很乱且难以查出错误 ...
- MVC扩展生成CheckBoxList并水平排列
本篇体验生成CheckBoxList的几个思路,扩展MVC的HtmlHelper生成CheckBoxList,并使之水平排开. 通过遍历从控制器方法拿到的Model集合 □ 思路 比如为一个用 ...
- MVC 自定义Htmlhelper扩展
在MVC中,我们不仅可以使用它原来的方法,我们还可以自定义,这不不仅加大了我们开发的效率,同时使界面更简洁. 具体什么是扩展方法,你可以这样理解,必须是静态且在形参中第一个参数是以this开头,大概先 ...
- Asp.Net MVC 扩展 Html.ImageFor 方法详解
背景: 在Asp.net MVC中定义模型的时候,DataType有DataType.ImageUrl这个类型,但htmlhelper却无法输出一个img,当用脚手架自动生成一些form或表格的时候, ...
- MVC中HtmlHelper用法大全参考
MVC中HtmlHelper用法大全参考 解析MVC中HtmlHelper控件7个大类中各个控件的主要使用方法(1) 2012-02-27 16:25 HtmlHelper类在命令System.Web ...
- MVC 扩展 Html.ImageFor
Asp.Net MVC 扩展 Html.ImageFor 方法详解 背景: 在Asp.net MVC中定义模型的时候,DataType有DataType.ImageUrl这个类型,但htmlhelpe ...
随机推荐
- VS自动编译脚本
rem ************************************************rem * Script to compile the solutions of IdealOE ...
- SpringBoot入门篇--Thymeleaf引擎模板的基本使用方法
我们在使用SpringBoot框架的时候在前面已经介绍了Thymelea引擎模板,因为SpringBoot对JSP惨不忍睹的支持.那我们在使用引擎模板对前端页面进行渲染能够返回的情况下我们怎么才能在静 ...
- float属性详解
内容: 1.block与inline复习 2.float介绍 3.float作用 4.清除浮动 1.block与inline复习 1 block元素是独立的一块,独占一行 2 多个block元素会各自 ...
- windows10系统telnet登陆
参考网站:https://jingyan.baidu.com/article/acf728fd498e9ff8e510a322.html windows10系统以及没有telnet服务器端了. 需要单 ...
- hbase命名空间
在HBase中,namespace命名空间指对一组表的逻辑分组,类似于数据库,便于对表在业务上划分 HBase系统默认定义了两个缺省的namespace hbase:系统内建表,包括namespace ...
- js1:根据标签的Id获取value值
例子:<input id="startDate" name="startDate" value="2015-09-14" class= ...
- Effective C++笔记——day01
1.当我们看到赋值符号时,请小心,因为"="也可以用来调用copy构造函数 Widget w3 = w2; //调用copy构造函数,而不是copy赋值操作符 2.不明确的行为: ...
- PAXOS/RAFT理解
PAXOS学习记录 前提: 信息准确无篡改,通信环境可信 目的: 解决多结点间一致性问题(集群中一个修改或者申请成为主结点的提议) 主要角色: Proposer :提出议案(同时存在一个或者多个,他们 ...
- json decimal and datetime
python json模块默认不能序列化decimal和datetime数据,可以通过自定义一个序列化的类实现: link: http://www.cnblogs.com/buxizhizhoum/p ...
- 使用ngxtop实时监控nginx
Ngxtop实时解析nginx访问日志,并且将处理结果输出到终端,功能类似于系统命令top,所以这个软件起名ngxtop.有了ngxtop,你可以实时了解到当前nginx的访问状况,再也不需要tail ...