-
C# MVC 枚举转 SelectListItem
- <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 根据枚举生成下拉列表数据源
-
-
-
-
-
-
-
- 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 });
- }
- }
-
-
-
-
-
-
- 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 获取枚举的描述
-
-
-
-
-
-
- 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();
- }
-
相关阅读:
小公司的技术分享怎么搞
当他们说「独立思考」时,到底在说什么
java使用tika批量识别文件的真实mime类型
hibernate:Not all named parameters have been
mybatis出错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.xxx.yyy.dao.ProjectMapper.getById
Mysql Hibernate报错
tomcat中多个端口部署项目
Windows Server 2012多个winlogon.exe LogonUI.exe dwm.exe ChsIME.exe进程
springboot使用profile指定不同配置(尚硅谷)
springboot配置文件占位符(尚硅谷)
-
原文地址:https://www.cnblogs.com/sjqq/p/7354975.html
Copyright © 2020-2023
润新知