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

  1. <span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">
  2. </span>
  1. <span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">调用代码:</span>
  1. public ActionResult Index()
  2. {
  3. IList<SelectListItem> listItem = EnumKit.ToSelectList(typeof(OrderStatus), "全部");
  4. ViewBag.SelectListItem = listItem;
  5. IList<SelectListItem> SelectListItemDesc = EnumKit.ToSelectListByDesc(typeof(OrderStatus));
  6. ViewBag.SelectListItemDesc = SelectListItemDesc;
  7. // 获取描述特性的值
  8. string sendHuo = OrderStatus.发货.GetDescription();
  9. return View();
  10. }

最新文章

  1. 【转载】经典SQL语句大全
  2. [LeetCode] Wildcard Matching 字符串匹配,kmp,回溯,dp
  3. Express使用手记:核心入门
  4. ProcDump
  5. Java私有构造器
  6. 动态SQL之标签
  7. 北大ACM(POJ1753-Flip Game)
  8. 当你还在争夺移动支付的时候,我已经统一了IC卡市场
  9. Ocelot中文文档-入门
  10. 【毕业原版】-《贝德福特大学毕业证书》Bedfordhire一模一样原件
  11. servlet(3)
  12. Java8内置的函数式接口
  13. 第26月第20天 springboot
  14. Java 之 JavaScript (一)
  15. 使用scrapy爬取百度股票
  16. Python 函数内变量的作用域
  17. 【Spring Boot&amp;&amp;Spring Cloud系列】Spring Boot初识
  18. 2.6 《硬啃设计模式》第8章 复制不是很难 - 原型模式(Prototype Pattern)
  19. PAT 1082 Read Number in Chinese[难]
  20. LL(1)文法--递归下降程序

热门文章

  1. IP封包协议头/TCP协议头/TCP3次握手/TCP4次挥手/UDP协议头/ICMP协议头/HTTP协议(请求报文和响应报文)/IP地址/子网掩码(划分子网)/路由概念/MAC封包格式
  2. 查看mysql二进制日志报错问题
  3. Flash学习笔记(01)
  4. 29个非常流行的jQuery提示信息插件
  5. Leetcode 233.数字1的个数
  6. NYOJ-673悟空的难题~~水题~~
  7. NodeJS仿WebApi路由
  8. 蓝桥杯 算法提高 金属采集 [ 树形dp 经典 ]
  9. POJ 1780 【手工递归】【欧拉回路】
  10. Spring中基于AOP的XML架构