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 ...
随机推荐
- ESB初步配置文件认识
每个项目的都有各自的场景,但是其实往小处说,场景的处理基本都是很相似,之前做copy文件的程序,其实就是一种很常见的ETL的过程(转移文件,异构系统通过文件系统交换数据,存在数据同步). 了解一下ET ...
- 在 Linux 下使用mdadm创建 RAID 5
在 RAID 5 中,数据条带化后存储在分布式奇偶校验的多个磁盘上.分布式奇偶校验的条带化意味着它将奇偶校验信息和条带化数据分布在多个磁盘上,这样会有很好的数据冗余. 在 Linux 中配置 RAID ...
- npm WARN react-native-maps@0.14.0 requires a peer of react@>=15.4.0 but none was installed
install the react-native here comes a questions :: npm WARN react-native@0.41.2 requires a pe ...
- LINUX漏洞-安全防护--防火墙相关
漏洞扫描 https://blog.csdn.net/e_Inch_Photo/article/details/79072360 基本安全防范: https://blog.csdn.net/holmo ...
- windows2008r2共享文件夹设置方法
一,无法启用网络发现的方法 参考网站: http://www.jb51.net/os/windows/win2008/154631.html Function Discovery R ...
- jpa-入门.缓存配置ehcache.xml
<ehcache> <!-- Sets the path to the directory where cache .data files are created. If the p ...
- MySQL Windows 安装与配置
<01> 下载解压 MySQL 至 D盘, 将文件夹改名为 MySQL <02> 控制台状态下进入 MySQL/bin 目录 输入 mysqld -install 回车 安装 ...
- LiveBinding应用 dataBind 数据绑定
http://blog.csdn.net/embarcaderochina/article/details/50352193 firemonkey grid/listview dataBind,数据绑 ...
- getitem, setitem, delitem (把类实例化成字典的类型)
class Foo(object): def __init__(self): self.data = {} def __getitem__(self, key): ...
- remote Request
import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; ...