在我们做正常的MVC的开发中,一些基本的控件已经够用了,但是有时候我们需要用到库里面没有的一些控件,比如RadioButtonListFor和CheckBoxListFor这类的列表控件,在MVC库里面没提供,需要自己来扩展一下。我们通过MvcHtmlString扩展的控件,最终还是被转换为html标签的形式,所以扩展控件实质上还是拼标签。其中用到TagBuilder这个类,是mvc自带的生成标签字符窜的类,大家没事的时候搜一下看看具体用法。还有selectListItem这个类,主要包含键值属性和是否选中的属性 。以下代码只是根据个人思路来做的,仅供参考。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Routing;
using System.Web.UI; 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++;
} } }
}
}

下面是调用的方法

控制器里面代码:

       var list = new List<SelectListItem>();
list.Add(new SelectListItem() { Text = "aaa", Value = "", Selected = false });
list.Add(new SelectListItem() { Text = "bbb", Value = "", Selected = false });
list.Add(new SelectListItem() { Text = "ccc", Value = "", Selected = false });
list.Add(new SelectListItem() { Text = "ddd", Value = "", Selected = true });
ViewBag.list = list;

页面上的代码(两种方式):

(1)

 @{
var list = ViewBag.list as List<SelectListItem>; }
@Html.CheckBoxListFor(p=>p.AccountCode,list,3)//@Html.RadioButtonListFor(p=>p.AccountCode,list,3)

(2)

@Html.CheckBoxListFor(p=>p.AccountCode,"list",3)//@Html.RadioButtonListFor(p=>p.AccountCode,"list",3)

其中的3代表每行显示3个,可以改为其他值,默认是显示一行

大致的代码就这么多,有不懂的或者有更好想法的,可以把代码贴在回复下面。

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

  1. MVC 扩展RadioButtonListFor和CheckBoxListFor

    学习MVC时候前端通常会用到HtmlHelper,使得前端编码简便很多.我们可能会经常用到htmlHelper中一些的EditorFor,LabelFor,ValiationMessageFor, 发 ...

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

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

  3. mvc给html扩展方法:

    mvc给html扩展方法: 注意:扩展方法和所在的类都必须是 public static如果在页面直接使用新扩展的方法,需要web.config里把Web.Helper名称命名空间加上,页面才能访问到 ...

  4. 在ASP.NET MVC下扩展一个带验证的RadioButtonList

    在ASP.NET MVC4中,HtmlHelper为我们提供了Html.RadioButton()方法用来显示Radio Button单选按钮.如果想显示一组单选按钮,通常的做法是遍历一个集合把每个单 ...

  5. Asp.net Boilerplate之AbpSession扩展

    当前Abp版本1.2,项目类型为MVC5. 以属性的形式扩展AbpSession,并在"记住我"后,下次自动登录也能获取到扩展属性的值,版权归"角落的白板报"所 ...

  6. 恢复SQL Server被误删除的数据(再扩展)

    恢复SQL Server被误删除的数据(再扩展) 大家对本人之前的文章<恢复SQL Server被误删除的数据> 反应非常热烈,但是文章里的存储过程不能实现对备份出来的日志备份里所删数据的 ...

  7. .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法

    .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...

  8. .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类

    .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类 0x00 为什么要引入扩展方法 有的中间件功能比较简单,有的则比较复杂,并且依赖其它组件.除 ...

  9. 采用EntityFramework.Extended 对EF进行扩展(Entity Framework 延伸系列2)

    前言 Entity Framework 延伸系列目录 今天我们来讲讲EntityFramework.Extended 首先科普一下这个EntityFramework.Extended是什么,如下: 这 ...

随机推荐

  1. Java进击C#——语法之线程同步

    上一章我们讲到关于C#线程方向的应用.但是笔者并没有讲到多线程中的另一个知识点--同步.多线程的应用开发都有可能发生脏数据.同步的功能或多或少都会用到.本章就要来讲一下关于线程同步的问题.根据笔者这几 ...

  2. STM32F746的RTC使用

    1.RTC模块采用低速晶振外接始终:32.768KHz,如下图所示 2.配置RTC模块: 其中,Fck_apre.Fck_spre始终上配置不容易理解, 如果想得到1Hz的始终频率,则需要将PERDI ...

  3. Nginx最大客户连接数算法一些遐想

    Nginx最大客户连接数算法一些遐想 现在很多互联网公司都在使用nginx,并且替换掉以前的Apache,nginx的优点就不说了,浅聊两句nginx的某些配置参数,找到这些参数设置的目的和关联性,并 ...

  4. react-native 简单的导航

    默默潜水了两年了,一直都在看大神们写的博客,现在我也分享一下跟RN导航有关的东西. 前两年我主要是做iOS开发的,现在刚找了份工作,应公司要求,现在开始学习reactnative的东西,由于我以前没怎 ...

  5. c#生成静态html文件,封装类

    由于这段时间比较轻松,于是想到很多的企业网站,新闻网站需要将页面静态化,于是写了个封装类来实现静态文件的生成,思路比较简单,但未完善,网友可根据自己的思路将此类扩展,运用了简单工厂模式(本来刚开始看设 ...

  6. Jedis的使用

    Redis是常用的key-value存储服务器,Java使用Redis有很多方法,其中官方推荐的是Jedis. 使用Jedis,首先是下载jedis-x.x.x.jar文件并导入工程,然后运行Redi ...

  7. Django模型类Meta元数据详解

    转自:https://my.oschina.net/liuyuantao/blog/751337 简介 使用内部的class Meta 定义模型的元数据,例如: from django.db impo ...

  8. 浅谈Collection集合

    俗话说:一个东西,一件事都离不开三句话:"是什么,为什么,怎么办" 集合是什么: 集合简单的说一个数组集合的高级体现,用来存储数据或对象的容器: 集合为什么存在: 集合只是体现了对 ...

  9. python之最强王者(11)——异常(exception)

    1.Python 异常处理 python提供了两个非常重要的功能来处理python程序在运行中出现的异常和错误.你可以使用该功能来调试python程序. 异常处理: 本站Python教程会具体介绍. ...

  10. [更新设计]跨平台物联网通讯框架ServerSuperIO 2.0 ,功能、BUG、细节说明,以及升级思考过程!

    注:ServerSuperIO 2.0 还没有提交到开源社区,在内部测试!!! 1. ServerSuperIO(SSIO)说明 SSIO是基于早期工业现场300波特率通讯传输应用场景发展.演化而来. ...