博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
扩展RadioButtonListFor和CheckBoxListFor
阅读量:4580 次
发布时间:2019-06-09

本文共 4640 字,大约阅读时间需要 15 分钟。

         在我们做正常的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    {///         /// 扩展radiobutton 列表        ///         /// 
实体
///
属性
/// /// 表达式 /// 数据列表 /// 每行显示个数 /// html属性 ///
public static MvcHtmlString RadioButtonListFor
(this HtmlHelper
helper, Expression
> expression, IEnumerable
items, int column = 0, object attributes = null) { string raidobuttonStr = ""; BuildListTag(out raidobuttonStr, "radio", items, expression, column, attributes); return MvcHtmlString.Create(raidobuttonStr); } ///
/// 扩展radiobutton 列表 /// ///
实体
///
属性
///
///
表达式 ///
viewData数据列表名称 ///
每行显示个数 ///
属性 ///
public static MvcHtmlString RadioButtonListFor
(this HtmlHelper
helper, Expression
> expression, string viewDataName, int column = 0, object attributes = null) { string raidobuttonStr = ""; var items = helper.ViewData[viewDataName] as List
; BuildListTag(out raidobuttonStr, "radio", items, expression, column, attributes); return MvcHtmlString.Create(raidobuttonStr); } ///
/// 扩展radiobutton 列表 /// ///
实体
///
属性
///
///
表达式 ///
数据列表 ///
每行显示个数 ///
html属性 ///
public static MvcHtmlString CheckBoxListFor
(this HtmlHelper
helper, Expression
> expression, IEnumerable
items, int column = 0, object attributes = null) { string raidobuttonStr = ""; BuildListTag(out raidobuttonStr, "checkbox", items, expression, column, attributes); return MvcHtmlString.Create(raidobuttonStr); } ///
/// 扩展radiobutton 列表 /// ///
实体
///
属性
///
///
表达式 ///
viewData数据列表名称 ///
每行显示个数 ///
属性 ///
public static MvcHtmlString CheckBoxListFor
(this HtmlHelper
helper, Expression
> expression, string viewDataName, int column = 0, object attributes = null) { string raidobuttonStr = ""; var items = helper.ViewData[viewDataName] as List
; BuildListTag(out raidobuttonStr, "checkbox", items, expression, column, attributes); return MvcHtmlString.Create(raidobuttonStr); } ///
/// 构造radioList或者checkBoxList标签 /// ///
///
///
拼接的字符窜 ///
标签(checkbox or radio) ///
表达式 ///
数据列表 ///
每行显示个数 ///
属性 private static void BuildListTag
(out string raidobuttonStr, string tag, IEnumerable
items, Expression
> expression, int column = 0, object attributes = null) { raidobuttonStr = ""; if (items != null && items.Any()) { int count = 1; ///获取表达式属性名称 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 == 1) { raidobuttonStr += "
"; } ///根据每行显示个数设置换行 else { if (count == column && column != 0) { raidobuttonStr += "
"; } } count++; } } } }}

下面是调用的方法  

   控制器里面代码: 

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

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

  (1)

@{    var list = ViewBag.list as List
; }@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个,可以改为其他值,默认是显示一行

 

 

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

 

                       

  

转载于:https://www.cnblogs.com/a546558309/p/4554592.html

你可能感兴趣的文章
怎么让table中的<td>内容向上对齐
查看>>
[Java] 遍历HashMap和HashMap转换成List的两种方式
查看>>
mongodb
查看>>
LeetCode 46. Permutations
查看>>
jmeter- 性能测试3:聚合报告(Aggregate Report )
查看>>
JavaScript高级程序设计---学习笔记(二)
查看>>
vim 插件的学习
查看>>
Uncaught SyntaxError: Unexpected token ILLEGAL
查看>>
一个预处理定义的问题
查看>>
ANDROID L——Material Design综合应用(Demo)
查看>>
自我介绍以及关于软件工程的问题
查看>>
struts (一)
查看>>
【新番推荐】工作细胞
查看>>
NYOJ 16 矩形嵌套
查看>>
Leetcode中的SQL题目练习(二)
查看>>
dubbo 集群容错源码
查看>>
Collection接口的子接口——Queue接口
查看>>
LINUX安装NGINX
查看>>
服务器启动项目抛错 没有到主机的路由
查看>>
python_85_sys模块
查看>>