C# MVC 枚举转 SelectListItem
2024-08-23 22:58:26
- <span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span><pre name="code" class="csharp">
- public static class EnumKit
- {
- #region 根据枚举生成下拉列表数据源
- /// <summary>
- /// 根据枚举生成下拉列表的数据源
- /// </summary>
- /// <param name="enumType">枚举类型</param>
- /// <param name="firstText">第一行文本(一般用于查询。例如:全部/请选择)</param>
- /// <param name="firstValue">第一行值(一般用于查询。例如:全部/请选择的值)</param>
- /// <returns></returns>
- public static IList<SelectListItem> ToSelectList(Type enumType
- , string firstText = "请选择"
- , string firstValue = "-1")
- {
- IList<SelectListItem> listItem = new List<SelectListItem>();
- if (enumType.IsEnum)
- {
- AddFirst(listItem, firstText, firstValue);
- Array values = Enum.GetValues(enumType);
- if (null != values && values.Length > 0)
- {
- foreach (int item in values)
- {
- listItem.Add(new SelectListItem { Value = item.ToString(), Text = Enum.GetName(enumType, item) });
- }
- }
- }
- else
- {
- throw new ArgumentException("请传入正确的枚举!");
- }
- return listItem;
- }
- static void AddFirst(IList<SelectListItem> listItem, string firstText, string firstValue)
- {
- if (!string.IsNullOrWhiteSpace(firstText))
- {
- if (string.IsNullOrWhiteSpace(firstValue))
- firstValue = "-1";
- listItem.Add(new SelectListItem { Text = firstText, Value = firstValue });
- }
- }
- /// <summary>
- /// 根据枚举的描述生成下拉列表的数据源
- /// </summary>
- /// <param name="enumType"></param>
- /// <returns></returns>
- public static IList<SelectListItem> ToSelectListByDesc(
- Type enumType
- , string firstText = "请选择"
- , string firstValue = "-1"
- )
- {
- IList<SelectListItem> listItem = new List<SelectListItem>();
- if (enumType.IsEnum)
- {
- AddFirst(listItem, firstText, firstValue);
- string[] names = Enum.GetNames(enumType);
- names.ToList().ForEach(item =>
- {
- string description = string.Empty;
- var field = enumType.GetField(item);
- object[] arr = field.GetCustomAttributes(typeof(DescriptionAttribute), true); //获取属性字段数组
- description = arr != null && arr.Length > 0 ? ((DescriptionAttribute)arr[0]).Description : item; //属性描述
- listItem.Add(new SelectListItem() { Value = ((int)Enum.Parse(enumType, item)).ToString(), Text = description });
- });
- }
- else
- {
- throw new ArgumentException("请传入正确的枚举!");
- }
- return listItem;
- }
- #endregion
- #region 获取枚举的描述
- /// <summary>
- /// 获取枚举的描述信息
- /// </summary>
- /// <param name="enumValue">枚举值</param>
- /// <returns>描述</returns>
- public static string GetDescription(this Enum enumValue)
- {
- string value = enumValue.ToString();
- FieldInfo field = enumValue.GetType().GetField(value);
- object[] objs = field.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
- if (objs == null || objs.Length == 0) return value;
- System.ComponentModel.DescriptionAttribute attr = (System.ComponentModel.DescriptionAttribute)objs[0];
- return attr.Description;
- }
- #endregion
- }
- <span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">
- </span>
- <span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">调用代码:</span>
- public ActionResult Index()
- {
- IList<SelectListItem> listItem = EnumKit.ToSelectList(typeof(OrderStatus), "全部");
- ViewBag.SelectListItem = listItem;
- IList<SelectListItem> SelectListItemDesc = EnumKit.ToSelectListByDesc(typeof(OrderStatus));
- ViewBag.SelectListItemDesc = SelectListItemDesc;
- // 获取描述特性的值
- string sendHuo = OrderStatus.发货.GetDescription();
- return View();
- }
最新文章
- 【转载】经典SQL语句大全
- [LeetCode] Wildcard Matching 字符串匹配,kmp,回溯,dp
- Express使用手记:核心入门
- ProcDump
- Java私有构造器
- 动态SQL之标签
- 北大ACM(POJ1753-Flip Game)
- 当你还在争夺移动支付的时候,我已经统一了IC卡市场
- Ocelot中文文档-入门
- 【毕业原版】-《贝德福特大学毕业证书》Bedfordhire一模一样原件
- servlet(3)
- Java8内置的函数式接口
- 第26月第20天 springboot
- Java 之 JavaScript (一)
- 使用scrapy爬取百度股票
- Python 函数内变量的作用域
- 【Spring Boot&;&;Spring Cloud系列】Spring Boot初识
- 2.6 《硬啃设计模式》第8章 复制不是很难 - 原型模式(Prototype Pattern)
- PAT 1082 Read Number in Chinese[难]
- LL(1)文法--递归下降程序
热门文章
- IP封包协议头/TCP协议头/TCP3次握手/TCP4次挥手/UDP协议头/ICMP协议头/HTTP协议(请求报文和响应报文)/IP地址/子网掩码(划分子网)/路由概念/MAC封包格式
- 查看mysql二进制日志报错问题
- Flash学习笔记(01)
- 29个非常流行的jQuery提示信息插件
- Leetcode 233.数字1的个数
- NYOJ-673悟空的难题~~水题~~
- NodeJS仿WebApi路由
- 蓝桥杯 算法提高 金属采集 [ 树形dp 经典 ]
- POJ 1780 【手工递归】【欧拉回路】
- Spring中基于AOP的XML架构