爱上MVC~为CheckBoxFor和RadioButtonFor加个扩展方法吧(希望MVC5把这方法收纳——呵呵)
说在前
我都是喜欢把问题复杂化,还有总是喜欢把问题简单化,偷懒化,这也需就是一个程序员的追求吧,呵呵。
我不太喜欢重复的东西,当你看到页面上有一个以上相同的代码时,那可以说,你的代码有重构的余地,应该考虑重构了,今天看了以前同事的博客,写了关于DropDownList的用法,如何将集合数据绑定到下拉列表框上,讲的不错,但最后在说checkbox和radiobutton时,我感觉有点重复了,我说的是代码重复了,还有就是代码复杂化了,就是说,我再使用集合来产生checkbox和radiobutton时,代码有些复杂了,这不是我们希望看到的,所以,我觉得有必要把它重构一下,为mvc加两个扩展方法吧,呵呵。
做在后
DropDownListFor的方法签名做的不错,很面向对象,很简洁,但不知不何,checkbox和radiobutton确没有这样的重载,呵呵
public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList); public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, IDictionary<string, object> htmlAttributes); public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, object htmlAttributes); public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel); public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel, IDictionary<string, object> htmlAttributes); public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel, object htmlAttributes);
对于checkbox和radiobutton它们只有简单的几个重载,没有对集合的支持,所以,我的这次代码扩展,主要是针对集合来说的,看方法签名:
下面看一下原代码,它直接写在了System.Web.Mvc命名空间下,这更说明我是对微软MVC的一个扩展,希望MVC5出来时,可以把这些都集成进去,呵呵
#region 单选框和复选框的扩展 /// <summary> /// 复选框,selValue为选中项 /// </summary> /// <param name="htmlHelper"></param> /// <param name="name"></param> /// <param name="selectList"></param> /// <param name="selValue"></param> /// <returns></returns> public static MvcHtmlString CheckBox(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, IEnumerable<string> selValue) { return CheckBoxAndRadioFor<object, string>(name, selectList, false, selValue); } public static MvcHtmlString CheckBox(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, string selValue) { return CheckBox(htmlHelper, name, selectList, new List<string> { selValue }); } /// <summary> /// 复选框 /// </summary> /// <param name="htmlHelper"></param> /// <param name="name"></param> /// <param name="selectList"></param> /// <returns></returns> public static MvcHtmlString CheckBoxFor(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList) { return CheckBox(htmlHelper, name, selectList, new List<string>()); } /// <summary> /// 根据列表输出checkbox /// </summary> /// <typeparam name="TModel"></typeparam> /// <typeparam name="TProperty"></typeparam> /// <param name="htmlHelper"></param> /// <param name="expression"></param> /// <param name="selectList"></param> /// <returns></returns> public static MvcHtmlString CheckBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList) { return CheckBoxFor(htmlHelper, expression, selectList, null); } /// <summary> /// 根据列表输出checkbox,selValue为默认选中的项 /// </summary> /// <typeparam name="TModel"></typeparam> /// <typeparam name="TProperty"></typeparam> /// <param name="htmlHelper"></param> /// <param name="expression"></param> /// <param name="selectList"></param> /// <param name="selValue"></param> /// <returns></returns> public static MvcHtmlString CheckBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string selValue) { string name = ExpressionHelper.GetExpressionText(expression); return CheckBoxAndRadioFor<TModel, TProperty>(name, selectList, false, new List<string> { selValue }); } /// <summary> /// 输出单选框和复选框 /// </summary> /// <typeparam name="TModel"></typeparam> /// <typeparam name="TProperty"></typeparam> /// <param name="expression"></param> /// <param name="selectList"></param> /// <param name="isRadio"></param> /// <param name="selValue"></param> /// <returns></returns> static MvcHtmlString CheckBoxAndRadioFor<TModel, TProperty>( string name, IEnumerable<SelectListItem> selectList, bool isRadio, IEnumerable<string> selValue) { StringBuilder str = new StringBuilder(); int c = 0; string check, activeClass; string type = isRadio ? "Radio" : "checkbox"; foreach (var item in selectList) { c++; if (selValue != null && selValue.Contains(item.Value)) { check = "checked='checked'"; activeClass = "style=color:red"; } else { check = string.Empty; activeClass = string.Empty; } str.AppendFormat("<span><input type='{3}' value='{0}' name={1} id={1}{2} " + check + "/>", item.Value, name, c, type); str.AppendFormat("<label for='{0}{1}' {3}>{2}</lable></span>", name, c, item.Text, activeClass); } return MvcHtmlString.Create(str.ToString()); } public static MvcHtmlString RadioButton(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, IEnumerable<string> selValue) { return CheckBoxAndRadioFor<object, string>(name, selectList, true, selValue); } /// <summary> /// 单选按钮组,seletList为选中项 /// </summary> /// <param name="htmlHelper"></param> /// <param name="name"></param> /// <param name="selectList"></param> /// <param name="selValue"></param> /// <returns></returns> public static MvcHtmlString RadioButton(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, string selValue) { return RadioButton(htmlHelper, name, selectList, new List<string> { selValue }); } /// <summary> /// 单选按钮组 /// </summary> /// <param name="htmlHelper"></param> /// <param name="name"></param> /// <param name="selectList"></param> /// <returns></returns> public static MvcHtmlString RadioButton(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList) { return RadioButton(htmlHelper, name, selectList, new List<string>()); } /// <summary> /// 根据列表输出radiobutton /// </summary> /// <typeparam name="TModel"></typeparam> /// <typeparam name="TProperty"></typeparam> /// <param name="htmlHelper"></param> /// <param name="expression"></param> /// <param name="selectList"></param> /// <returns></returns> public static MvcHtmlString RadioButtonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList) { return RadioButtonFor(htmlHelper, expression, selectList, new List<string>()); } public static MvcHtmlString RadioButtonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, IEnumerable<string> selValue) { string name = ExpressionHelper.GetExpressionText(expression); return CheckBoxAndRadioFor<TModel, TProperty>(name, selectList, true, selValue); } /// <summary> /// 根据列表输出radiobutton,selValue为默认选中的项 /// </summary> /// <typeparam name="TModel"></typeparam> /// <typeparam name="TProperty"></typeparam> /// <param name="htmlHelper"></param> /// <param name="expression"></param> /// <param name="selectList"></param> /// <param name="selValue"></param> /// <returns></returns> public static MvcHtmlString RadioButtonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string selValue) { return RadioButtonFor(htmlHelper, expression, selectList, new List<string> { selValue }); } #endregion
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
2012-12-17 数据结构~工作流的设计(链表结构)
2012-12-17 爱上MVC3~实体级标准验证
2011-12-17 LINQ TO SQL三层架构~更新操作
2011-12-17 LINQ TO SQL三层架构~添加操作