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. }

C# MVC 枚举转 SelectListItem的更多相关文章

  1. MVC 枚举 转 SelectListItem

    ViewBag.userlevel = new SelectList(Enum.GetNames(typeof(AdminLevels)),"", "", te ...

  2. C#.NET MVC 枚举转dictionary自动装载生成下拉框

      /// <summary> /// 枚举转SelectListItem /// </summary> public class Enum_Helper { /// < ...

  3. ASP.NET MVC 枚举类型转LIST CONTROL控件

    在实际应用中,我们经常会用到下拉框.多选.单选等类似的控件,我们可以统称他们为List Control,他们可以说都是一种类型的控件,相同之处都是由一个或一组键值对的形式的数据进行绑定渲染而成的. 这 ...

  4. MVC 枚举绑定 DropDownList

    /// <summary> /// 枚举转化下拉列表数据集 /// </summary> /// <param name="type">类型&l ...

  5. asp.net MVC 枚举类型的处理的几种方式

    枚举类型本质上是int类型,整型,这是非常重要的一点. 可以使用(int)将它强制转换为 整形.如果要使用MVC5提供的新辅助方法@Html.EnumDropDownListFor()方法,就必须将枚 ...

  6. .net MVC 中枚举类型Enum 转化成 下拉列表的数据源

    第一次写技术博文,记录下工作中遇到的问题,给自己的知识做个备份,也希望能帮助到其他的同学 最近接手了公司的一个新的项目.有个页面涉及相关设计. 分享一个经常用到的吧. 方法一: 直入主题吧 我们的目的 ...

  7. MVC中下拉框显示枚举项

    原文:MVC中下拉框显示枚举项 本篇将通过3种方式,把枚举项上的自定义属性填充到下拉框: 1.通过控制器返回List<SelectListItem>类型给前台视图 2.通过为枚举类型属性打 ...

  8. 你想要的都在这里,ASP.NET Core MVC四种枚举绑定方式

    前言 本节我们来讲讲在ASP.NET Core MVC又为我们提供了哪些方便,之前我们探讨过在ASP.NET MVC中下拉框绑定方式,这节我们来再来重点看看枚举绑定的方式,充分实现你所能想到的场景,满 ...

  9. ASP.NET Core MVC四种枚举绑定方式

    前言 本节我们来讲讲在ASP.NET Core MVC又为我们提供了哪些方便,之前我们探讨过在ASP.NET MVC中下拉框绑定方式,这节我们来再来重点看看枚举绑定的方式,充分实现你所能想到的场景,满 ...

随机推荐

  1. mysql语句中判断是否包含某字符串的方法

    当我们需要对数据做筛选和查询的时候,往往会涉及到一些限制条件的判断,今天就分享一个判断字符串的技巧. like 相信大家对like的用法肯定都很熟悉了,它可以匹配字段以某字符串开始,以某字符串结尾,包 ...

  2. 剑指Offer(书):旋转数组的最小数字

    题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转, ...

  3. STM32开发环境--使用MDK建立一个工程

    STM32开发环境--使用MDK建立一个工程 该工程模版是基于库函数基础制作而成,其中有借鉴相关资料.虽然工程模版一旦建立,以后任何项目只需套用即可,但考虑到长时间不使用,在将来某天可能会突然用到,再 ...

  4. SHELL十三问[转载自CU论坛]

    原文地址:http://bbs.chinaunix.net/thread-218853-1-1.html 一.为什么称作shell? http://bbs.chinaunix.net/viewthr ...

  5. jar包、war包、ear包傻傻分不清?

    在工作中,需要在jboss上deploy一个health check的war包,因此了解一下: Jar文件(扩展名为. Jar,Java Application Archive)包含Java类的普通库 ...

  6. javascript、jquery 、C#、sqlserveer、mysql、oracle中字符串截取的区别和用法

    下标从0开始 ,并且包括起始位 javascript 中字符串截取 : substring(Number start,Number end) var substr = "liuguangfa ...

  7. bzoj 2818 GCD 数论 欧拉函数

    bzoj[2818]Gcd Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Samp ...

  8. ubuntu 18.04取消自动锁屏以及设置键盘快捷锁屏

    1:操作设置取消自动锁屏: setting-->power--->never 2:  设置自动锁屏快捷键: 快捷键设置一般在setting-->devices--->keybo ...

  9. Object_C 定义全局宏的颜色时,报“Expected identifier”的错误

    在定义全局颜色宏的时候,为了整齐把空格删了,写在了同一行里,调用的时候,出错提示“Expected identifier”,如下: 如果宏定义如上那样的话,在调用的时候,会出现如下的问题: 百思不得解 ...

  10. Educational Codeforces Round 50 (Rated for Div. 2)F. Relatively Prime Powers

    实际上就是求在[2,n]中,x != a^b的个数,那么实际上就是要求x=a^b的个数,然后用总数减掉就好了. 直接开方求和显然会有重复的数.容斥搞一下,但实际上是要用到莫比乌斯函数的,另外要注意减掉 ...