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. (12) OpenSSL主配置文件openssl.cnf

    1.man config 该帮助文档说明了openssl.cnf以及一些其他辅助配置文件的规范.格式及读取方式.后文中的所有解释除非特别指明,都将以openssl.cnf为例. [root@local ...

  2. CSS---基础外部样式表

    <link rel="stylesheet" type="text/css" href="style.css" media=" ...

  3. LeetCode(19) Remove Nth Node From End of List

    题目 Given a linked list, remove the nth node from the end of list and return its head. For example, G ...

  4. string和数值之间的转换

    string和数值之间的转换 to_string(val) 一组重载函数,返回数值val的string表示val可以是任何算数类型. stoi(s,p,b),stol(s,p,b),stoul(s,p ...

  5. 如何在小程序自定义组件和动态传入数据小demo

    在开发过程中,我们会将页面常用功能抽象为一个组件,这样既方便又可以避免代码冗余.小程序中也提供了自定义组件,了解Vue的伙伴们会发现其实和Vue的组件化很相似.这里用返回顶部功能来说说如何自定义组件, ...

  6. Java基础学习总结(94)——Java线程再学习

    Java线程有哪些不太为人所知的技巧与用法? 萝卜白菜各有所爱.像我就喜欢Java.学无止境,这也是我喜欢它的一个原因.日常工作中你所用到的工具,通常都有些你从来没有了解过的东西,比方说某个方法或者是 ...

  7. BestCoder Round #90 A+B题解!

    BestCoder Round #90 A  Kblack loves flag 题意有点迷不造思路很简单但不造怎么求随机数,纠结了一会后直接粘上题目所给的代码稍加修改A了. const int _K ...

  8. CodeForces 610B-Vika and Squares,有坑点,不是很难~~

    B. Vika and Squares time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. hdu1059(背包dp二进制优化)

    Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  10. iview自定义配置

    说明 iview是一套基于 Vue.js 的高质量 UI 组件库.主要用户PC端页面设计. 官网:https://www.iviewui.com/ 1.在vue-cli项目中,添加该框架 第一步,安装 ...