1 /// <summary> 2 /// 枚举转化下拉列表数据集 3 /// </summary> 4 /// <param name="type">类型</param> 5 /// <param name="selected">选中项</param> 6 /// <returns>结果</returns> 7 public static IEnumerable<SelectListItem> EnumToListItem(Type type, string selected) 8 { 9 IList<SelectListItem> list = new List<SelectListItem> { new SelectListItem { Text = "全部", Value = string.Empty } }; 10 Array array = Enum.GetValues(type); 11 foreach (int item in array) 12 { 13 string text = Enum.GetName(type, item); 14 var selectListItem = new SelectListItem 15 { 16 Text = text, 17 Value = item.ToString(CultureInfo.InvariantCulture), 18 Selected = !string.IsNullOrEmpty(selected) && selected == text 19 }; 20 list.Add(selectListItem); 21 } 22 23 return list; 24 } 25 26 /// <summary> 27 /// 自定义DropDownList 28 /// </summary> 29 /// <param name="htmlHelper">htmlHelper</param> 30 /// <param name="name">name</param> 31 /// <param name="selectList">下拉列表集合</param> 32 /// <param name="obj">属性</param> 33 /// <returns>MvcHtmlString</returns> 34 public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, object obj) 35 { 36 string fullName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name); 37 if (string.IsNullOrEmpty(fullName)) 38 { 39 throw new ArgumentException("name"); 40 } 41 42 StringBuilder listItemBuilder = new StringBuilder(); 43 foreach (SelectListItem item in selectList) 44 { 45 TagBuilder builder = new TagBuilder("option") 46 { 47 InnerHtml = HttpUtility.HtmlEncode(item.Text) 48 }; 49 50 if (item.Value != null) 51 { 52 builder.Attributes["value"] = item.Value; 53 } 54 55 if (item.Selected) 56 { 57 builder.Attributes["selected"] = "selected"; 58 } 59 60 listItemBuilder.AppendLine(builder.ToString(TagRenderMode.Normal)); 61 } 62 63 TagBuilder tagBuilder = new TagBuilder("select") 64 { 65 InnerHtml = listItemBuilder.ToString() 66 }; 67 68 if (obj != null) 69 { 70 IDictionary<string, object> htmlAttributes = new RouteValueDictionary(obj); 71 tagBuilder.MergeAttributes(htmlAttributes); 72 } 73 tagBuilder.MergeAttribute("name", fullName, true); 74 tagBuilder.GenerateId(fullName); 75 76 return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal)); 77 } 78 79 /// <summary> 80 /// 自定义DropDownList 81 /// </summary> 82 /// <param name="htmlHelper">htmlHelper</param> 83 /// <param name="name">name</param> 84 /// <param name="selectList">下拉列表集合</param> 85 /// <returns>MvcHtmlString</returns> 86 public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList) 87 { 88 return DropDownList(htmlHelper, name, selectList, null); 89 } 90 91 /// <summary> 92 /// 自定义DropDownList 93 /// </summary> 94 /// <param name="htmlHelper">htmlHelper</param> 95 /// <param name="type">枚举类型</param> 96 /// <param name="name">name属性</param> 97 /// <param name="selected">选择项目</param> 98 /// <param name="obj">其他属性</param> 99 /// <returns>DropDownList</returns> 100 public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, Type type, string name, string selected, object obj) 101 { 102 IEnumerable<SelectListItem> selectListItem = EnumToListItem(type, selected); 103 return DropDownList(htmlHelper, name, selectListItem, obj); 104 } 105 106 /// <summary> 107 /// 自定义DropDownList 108 /// </summary> 109 /// <param name="htmlHelper">htmlHelper</param> 110 /// <param name="type">枚举类型</param> 111 /// <param name="name">name属性</param> 112 /// <param name="selected">选择项目</param> 113 /// <returns>DropDownList</returns> 114 public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, Type type, string name, string selected) 115 { 116 return DropDownList(htmlHelper, type, name, selected, null); 117 } 118 119 /// <summary> 120 /// 自定义DropDownList 121 /// </summary> 122 /// <param name="htmlHelper">htmlHelper</param> 123 /// <param name="type">枚举类型</param> 124 /// <param name="name">name属性</param> 125 /// <param name="obj">其他属性</param> 126 /// <returns>DropDownList</returns> 127 public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, Type type, string name, object obj) 128 { 129 return DropDownList(htmlHelper, type, name, null, obj); 130 } 131 132 /// <summary> 133 /// 自定义DropDownList 134 /// </summary> 135 /// <param name="htmlHelper">htmlHelper</param> 136 /// <param name="type">枚举类型</param> 137 /// <param name="name">name属性</param> 138 /// <returns>DropDownList</returns> 139 public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, Type type, string name) 140 { 141 return DropDownList(htmlHelper, type, name, null, null); 142 }
时间: 2024-10-03 19:23:24