1 /// <summary> 2 /// 将enum转换成List<Model.Models.SelectViewModels>,即html的select标签使用的数据 3 /// </summary> 4 /// <returns></returns> 5 public static List<Model.Models.SelectViewModels> EnumToSelect(Type t) 6 { 7 List<Model.Models.SelectViewModels> select = new List<Model.Models.SelectViewModels>(); 8 foreach (var e in Enum.GetValues(t)) 9 { 10 Model.Models.SelectViewModels model = new Model.Models.SelectViewModels(); 11 model.Text = e.ToString(); 12 model.Value = Enum.Format(t, e, "d"); 13 select.Add(model); 14 } 15 return select; 16 }
使用属性器获得属性值
public enum RedPackStatus { [Description("发放中")] SENDING, [Description("已发放待领取")] SENT, [Description("发放失败")] FAILED, [Description("已领取")] RECEIVED, [Description("退款中")] RFUND_ING, [Description("已退款")] REFUND, } /// <summary> /// 获取枚举描述信息 /// </summary> /// <param name="enumType">枚举类型</param> /// <param name="enumStr">枚举字值字符串</param> /// <returns></returns> public static string GetEnumDescription(Type enumType, string enumStr) { FieldInfo p = enumType.GetField(enumStr); object[] os = p.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), true); if (os == null || os.Length == 0) { return null; } return ((DescriptionAttribute)os[0]).Description; } public ActionResult Index(string id) { string a = Common.EnumToList.GetEnumDescription(typeof(Utility.Enums.WeiXin.RedPackStatus), "SENDING"); return Content("a:"+a); }