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 根据枚举生成下拉列表数据源
- /// <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();
- }
C# MVC 枚举转 SelectListItem的更多相关文章
- MVC 枚举 转 SelectListItem
ViewBag.userlevel = new SelectList(Enum.GetNames(typeof(AdminLevels)),"", "", te ...
- C#.NET MVC 枚举转dictionary自动装载生成下拉框
/// <summary> /// 枚举转SelectListItem /// </summary> public class Enum_Helper { /// < ...
- ASP.NET MVC 枚举类型转LIST CONTROL控件
在实际应用中,我们经常会用到下拉框.多选.单选等类似的控件,我们可以统称他们为List Control,他们可以说都是一种类型的控件,相同之处都是由一个或一组键值对的形式的数据进行绑定渲染而成的. 这 ...
- MVC 枚举绑定 DropDownList
/// <summary> /// 枚举转化下拉列表数据集 /// </summary> /// <param name="type">类型&l ...
- asp.net MVC 枚举类型的处理的几种方式
枚举类型本质上是int类型,整型,这是非常重要的一点. 可以使用(int)将它强制转换为 整形.如果要使用MVC5提供的新辅助方法@Html.EnumDropDownListFor()方法,就必须将枚 ...
- .net MVC 中枚举类型Enum 转化成 下拉列表的数据源
第一次写技术博文,记录下工作中遇到的问题,给自己的知识做个备份,也希望能帮助到其他的同学 最近接手了公司的一个新的项目.有个页面涉及相关设计. 分享一个经常用到的吧. 方法一: 直入主题吧 我们的目的 ...
- MVC中下拉框显示枚举项
原文:MVC中下拉框显示枚举项 本篇将通过3种方式,把枚举项上的自定义属性填充到下拉框: 1.通过控制器返回List<SelectListItem>类型给前台视图 2.通过为枚举类型属性打 ...
- 你想要的都在这里,ASP.NET Core MVC四种枚举绑定方式
前言 本节我们来讲讲在ASP.NET Core MVC又为我们提供了哪些方便,之前我们探讨过在ASP.NET MVC中下拉框绑定方式,这节我们来再来重点看看枚举绑定的方式,充分实现你所能想到的场景,满 ...
- ASP.NET Core MVC四种枚举绑定方式
前言 本节我们来讲讲在ASP.NET Core MVC又为我们提供了哪些方便,之前我们探讨过在ASP.NET MVC中下拉框绑定方式,这节我们来再来重点看看枚举绑定的方式,充分实现你所能想到的场景,满 ...
随机推荐
- (12) OpenSSL主配置文件openssl.cnf
1.man config 该帮助文档说明了openssl.cnf以及一些其他辅助配置文件的规范.格式及读取方式.后文中的所有解释除非特别指明,都将以openssl.cnf为例. [root@local ...
- CSS---基础外部样式表
<link rel="stylesheet" type="text/css" href="style.css" media=" ...
- 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 ...
- string和数值之间的转换
string和数值之间的转换 to_string(val) 一组重载函数,返回数值val的string表示val可以是任何算数类型. stoi(s,p,b),stol(s,p,b),stoul(s,p ...
- 如何在小程序自定义组件和动态传入数据小demo
在开发过程中,我们会将页面常用功能抽象为一个组件,这样既方便又可以避免代码冗余.小程序中也提供了自定义组件,了解Vue的伙伴们会发现其实和Vue的组件化很相似.这里用返回顶部功能来说说如何自定义组件, ...
- Java基础学习总结(94)——Java线程再学习
Java线程有哪些不太为人所知的技巧与用法? 萝卜白菜各有所爱.像我就喜欢Java.学无止境,这也是我喜欢它的一个原因.日常工作中你所用到的工具,通常都有些你从来没有了解过的东西,比方说某个方法或者是 ...
- BestCoder Round #90 A+B题解!
BestCoder Round #90 A Kblack loves flag 题意有点迷不造思路很简单但不造怎么求随机数,纠结了一会后直接粘上题目所给的代码稍加修改A了. const int _K ...
- CodeForces 610B-Vika and Squares,有坑点,不是很难~~
B. Vika and Squares time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- hdu1059(背包dp二进制优化)
Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- iview自定义配置
说明 iview是一套基于 Vue.js 的高质量 UI 组件库.主要用户PC端页面设计. 官网:https://www.iviewui.com/ 1.在vue-cli项目中,添加该框架 第一步,安装 ...