C# MVC 枚举转 SelectListItem

  1. <span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span><pre name="code" class="csharp">   
[csharp] view plain copy
 
 print?
  1. public static class EnumKit  
  2.    {  
  3.        #region 根据枚举生成下拉列表数据源  
  4.        /// <summary>  
  5.        /// 根据枚举生成下拉列表的数据源  
  6.        /// </summary>  
  7.        /// <param name="enumType">枚举类型</param>  
  8.        /// <param name="firstText">第一行文本(一般用于查询。例如:全部/请选择)</param>  
  9.        /// <param name="firstValue">第一行值(一般用于查询。例如:全部/请选择的值)</param>  
  10.        /// <returns></returns>  
  11.        public static IList<SelectListItem> ToSelectList(Type enumType  
  12.            , string firstText = "请选择"  
  13.            , string firstValue = "-1")  
  14.        {  
  15.            IList<SelectListItem> listItem = new List<SelectListItem>();  
  16.   
  17.            if (enumType.IsEnum)  
  18.            {  
  19.                AddFirst(listItem, firstText, firstValue);  
  20.   
  21.                Array values = Enum.GetValues(enumType);  
  22.                if (null != values && values.Length > 0)  
  23.                {  
  24.                    foreach (int item in values)  
  25.                    {  
  26.                        listItem.Add(new SelectListItem { Value = item.ToString(), Text = Enum.GetName(enumType, item) });  
  27.                    }  
  28.                }  
  29.            }  
  30.            else  
  31.            {  
  32.                throw new ArgumentException("请传入正确的枚举!");  
  33.            }  
  34.            return listItem;  
  35.        }  
  36.   
  37.        static void AddFirst(IList<SelectListItem> listItem, string firstText, string firstValue)  
  38.        {  
  39.            if (!string.IsNullOrWhiteSpace(firstText))  
  40.            {  
  41.                if (string.IsNullOrWhiteSpace(firstValue))  
  42.                    firstValue = "-1";  
  43.                listItem.Add(new SelectListItem { Text = firstText, Value = firstValue });  
  44.            }  
  45.        }  
  46.   
  47.        /// <summary>  
  48.        /// 根据枚举的描述生成下拉列表的数据源  
  49.        /// </summary>  
  50.        /// <param name="enumType"></param>  
  51.        /// <returns></returns>  
  52.        public static IList<SelectListItem> ToSelectListByDesc(  
  53.            Type enumType  
  54.            , string firstText = "请选择"  
  55.            , string firstValue = "-1"  
  56.            )  
  57.        {  
  58.            IList<SelectListItem> listItem = new List<SelectListItem>();  
  59.   
  60.            if (enumType.IsEnum)  
  61.            {  
  62.                AddFirst(listItem, firstText, firstValue);  
  63.                string[] names = Enum.GetNames(enumType);  
  64.                names.ToList().ForEach(item =>  
  65.                {  
  66.                    string description = string.Empty;  
  67.                    var field = enumType.GetField(item);  
  68.                    object[] arr = field.GetCustomAttributes(typeof(DescriptionAttribute), true); //获取属性字段数组    
  69.                    description = arr != null && arr.Length > 0 ? ((DescriptionAttribute)arr[0]).Description : item;   //属性描述    
  70.   
  71.                    listItem.Add(new SelectListItem() { Value = ((int)Enum.Parse(enumType, item)).ToString(), Text = description });  
  72.                });  
  73.            }  
  74.            else  
  75.            {  
  76.                throw new ArgumentException("请传入正确的枚举!");  
  77.            }  
  78.            return listItem;  
  79.        }  
  80.        #endregion  
  81.  
  82.        #region 获取枚举的描述  
  83.   
  84.        /// <summary>  
  85.        /// 获取枚举的描述信息  
  86.        /// </summary>  
  87.        /// <param name="enumValue">枚举值</param>  
  88.        /// <returns>描述</returns>  
  89.        public static string GetDescription(this Enum enumValue)  
  90.        {  
  91.            string value = enumValue.ToString();  
  92.            FieldInfo field = enumValue.GetType().GetField(value);  
  93.            object[] objs = field.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);  
  94.            if (objs == null || objs.Length == 0) return value;  
  95.            System.ComponentModel.DescriptionAttribute attr = (System.ComponentModel.DescriptionAttribute)objs[0];  
  96.            return attr.Description;  
  97.        }  
  98.  
  99.        #endregion  
  100.    }  




[csharp] view plain copy
 
 print?
  1. <span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">  
  2. </span>  
[csharp] view plain copy
 
 print?
  1. <span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">调用代码:</span>  
[csharp] view plain copy
 
 print?
  1. public ActionResult Index()  
  2.        {  
  3.            IList<SelectListItem> listItem = EnumKit.ToSelectList(typeof(OrderStatus), "全部");  
  4.            ViewBag.SelectListItem = listItem;  
  5.   
  6.   
  7.            IList<SelectListItem> SelectListItemDesc = EnumKit.ToSelectListByDesc(typeof(OrderStatus));  
  8.            ViewBag.SelectListItemDesc = SelectListItemDesc;  
  9.   
  10.   
  11.            // 获取描述特性的值  
  12.            string sendHuo = OrderStatus.发货.GetDescription();  
  13.   
  14.            return View();  
  15.        }  
原文地址:https://www.cnblogs.com/sjqq/p/7354975.html