枚举用于选择下拉框
1 /// <summary> 2 /// 将枚举绑定到下拉框 3 /// </summary> 4 /// <typeparam name="T"></typeparam> 5 /// <param name="p_Value">选中值</param> 6 /// <param name="p_Tip">提示语</param> 7 /// <param name="IsShowSelectTip">是否显示提示语</param> 8 /// <param name="RemoveIndex">排除不显示的枚举值(索引)</param> 9 /// <returns></returns> 10 public static SelectList EnumToListItem<T>(int? p_Value, string p_Tip = "", bool IsShowSelectTip = true, int RemoveIndex = -10) 11 { 12 object _selectValue = null; 13 if (p_Value.HasValue) 14 { 15 _selectValue = p_Value.Value; 16 } 17 var item = new List<SelectListItem>(); 18 if (IsShowSelectTip) 19 { 20 item.Add(new SelectListItem { Value = "", Text = "--请选择" + p_Tip + "--" }); 21 } 22 foreach (int s in Enum.GetValues(typeof(T))) 23 { 24 //item.Add(new SelectListItem() { Text = Enum.GetName(typeof(T), s), Value = s.ToString() }); 25 if (RemoveIndex != s) 26 { 27 item.Add(new SelectListItem { Text = Enum.GetName(typeof(T), s), Value = s.ToString() }); 28 } 29 } 30 var list = new SelectList(item, "Value", "Text", _selectValue); 31 return list; 32 }
将枚举转换为字典
1 /// <summary> 2 /// 将枚举转换成字典集合 3 /// </summary> 4 public static IDictionary<object, object> EnumToList<T>() 5 { 6 Dictionary<object, object> m_Dic = new Dictionary<object, object>(); 7 string m_Value = string.Empty; 8 foreach (int s in Enum.GetValues(typeof(T))) 9 { 10 m_Value = Enum.GetName(typeof(T), s); 11 m_Dic.Add(s, m_Value); 12 } 13 return m_Dic; 14 }