我都是喜欢把问题复杂化,还有总是喜欢把问题简单化,偷懒化,这也需就是一个程序员的追求吧,呵呵。
我不太喜欢重复的东西,当你看到页面上有一个以上相同的代码时,那可以说,你的代码有重构的余地,应该考虑重构了,今天看了以前同事的博客,写了关于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