项目中经常用到枚举,根据枚举定义的下拉框比如用户状态等,就非常麻烦,所以写个根据枚举项自动生成下拉框,并扩展htmlhelper类去显示枚举的值.
定义管理员状态枚举值:
1 /// <summary> 2 /// 管理员状态 3 /// </summary> 4 public enum ManageState 5 { 6 /// <summary> 7 /// 正常 8 /// </summary> 9 [Display(Name="正常")] 10 Normal = 0, 11 /// <summary> 12 /// 已禁用 13 /// </summary> 14 [Display(Name = "已禁用")] 15 Stop = 1 16 }
更具枚举生成下拉框:
简单helpers 方法:
@*将枚举变为下拉框*@ @helper DropEnumFor(Type enumType,string name="state",KeyValuePair<string,string>? defu=null){ Dictionary<string, int> dir = new Dictionary<string, int>(); var discriptType = typeof(System.ComponentModel.DataAnnotations.DisplayAttribute); var defultChecked = HttpContext.Current.Request["state"]; foreach (var filed in enumType.GetFields()) { if (!filed.FieldType.IsEnum) {continue;} var val = (int)enumType.InvokeMember(filed.Name, System.Reflection.BindingFlags.GetField, null, null, null); var key = ""; var attrs=filed.GetCustomAttributes(discriptType,true); if (attrs.Length > 0) { key = ((DisplayAttribute)attrs[0]).Name; } else { key = filed.Name; } dir.Add(key, val); } <select id="@name" name="@name"> @if (defu != null) { <option value="@defu.Value.Value">@defu.Value.Key</option> } @foreach (var item in dir.OrderByDescending(p=>p.Value)) { var check = string.Empty; if (defultChecked == item.Value.ToString()) { check = "selected=\"true\""; } <option value="@item.Value" @check >@item.Key</option> } </select> }
全局静态扩展:
1 /// <summary> 2 /// 获取枚举的下拉框 3 /// </summary> 4 /// <typeparam name="TEnum">枚举类型</typeparam> 5 /// <param name="name">空间name属性</param> 6 /// <param name="defaultValue">默认值</param> 7 /// <returns>select 下拉框</returns> 8 public static string DropEnumFor<TEnum>(this HtmlHelper helper, string name = "state", KeyValuePair<string, string>? defaultValue = null) 9 where TEnum : struct 10 { 11 var result = new StringBuilder("<select"); 12 var enumType = typeof(TEnum); 13 var defultChecked = HttpContext.Current.Request["state"]; 14 Dictionary<string, int> dir = new Dictionary<string, int>(); 15 var discriptType = typeof(System.ComponentModel.DataAnnotations.DisplayAttribute); 16 foreach (var filed in enumType.GetFields()) 17 { 18 if (!filed.FieldType.IsEnum) { continue; } 19 var val = (int)enumType.InvokeMember(filed.Name, System.Reflection.BindingFlags.GetField, null, null, null); 20 var key = ""; 21 var attrs = filed.GetCustomAttributes(discriptType, true); 22 if (attrs.Length > 0) 23 { 24 key = ((DisplayAttribute)attrs[0]).Name; 25 } 26 else 27 { 28 key = filed.Name; 29 } 30 dir.Add(key, val); 31 } 32 result.AppendFormat(" id=\"{0}\" name=\"{1}\">", name, name); 33 if (defaultValue != null) 34 { 35 36 } 37 foreach (var item in dir.OrderByDescending(p => p.Value)) 38 { 39 var check=string.Empty;; 40 if (defultChecked == item.Value.ToString()) 41 check = "selected=\"true\""; 42 result.AppendFormat("<option value=\"{0}\" {2}>{1}</option>", item.Value, item.Key, check); 43 } 44 result.Append("</select>"); 45 return result.ToString(); 46 } 47 }
扩展htmlhelper进行显示
/// <summary> /// 获取枚举的display属性 /// </summary> /// <param name="helper"></param> /// <param name="state"></param> /// <returns></returns> public static string Display<TEnum>(this HtmlHelper helper, TEnum state) where TEnum : struct { var enumType = typeof(TEnum); var discriptType = typeof(System.ComponentModel.DataAnnotations.DisplayAttribute); var enumName = enumType.GetEnumName(state); var result = enumName; var attributes= enumType.GetField(enumName).GetCustomAttributes(discriptType, true); if (attributes.Length <= 0) return result; var dispay=attributes[0] as System.ComponentModel.DataAnnotations.DisplayAttribute; result = dispay == null ? result : dispay.Name; return result; }
使用方式
下拉框:
@Helpers.DropEnumFor(typeof(ManageState), "state", new KeyValuePair<string, string>("全部", "0"))
显示:
<span>
[@Html.Display(item.ManageState)]
</span>
注:如果在反射时候加上缓存,效率会提升。