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中下拉框绑定方式,这节我们来再来重点看看枚举绑定的方式,充分实现你所能想到的场景,满 ...
随机推荐
- mysql语句中判断是否包含某字符串的方法
当我们需要对数据做筛选和查询的时候,往往会涉及到一些限制条件的判断,今天就分享一个判断字符串的技巧. like 相信大家对like的用法肯定都很熟悉了,它可以匹配字段以某字符串开始,以某字符串结尾,包 ...
- 剑指Offer(书):旋转数组的最小数字
题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转, ...
- STM32开发环境--使用MDK建立一个工程
STM32开发环境--使用MDK建立一个工程 该工程模版是基于库函数基础制作而成,其中有借鉴相关资料.虽然工程模版一旦建立,以后任何项目只需套用即可,但考虑到长时间不使用,在将来某天可能会突然用到,再 ...
- SHELL十三问[转载自CU论坛]
原文地址:http://bbs.chinaunix.net/thread-218853-1-1.html 一.为什么称作shell? http://bbs.chinaunix.net/viewthr ...
- jar包、war包、ear包傻傻分不清?
在工作中,需要在jboss上deploy一个health check的war包,因此了解一下: Jar文件(扩展名为. Jar,Java Application Archive)包含Java类的普通库 ...
- javascript、jquery 、C#、sqlserveer、mysql、oracle中字符串截取的区别和用法
下标从0开始 ,并且包括起始位 javascript 中字符串截取 : substring(Number start,Number end) var substr = "liuguangfa ...
- bzoj 2818 GCD 数论 欧拉函数
bzoj[2818]Gcd Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Samp ...
- ubuntu 18.04取消自动锁屏以及设置键盘快捷锁屏
1:操作设置取消自动锁屏: setting-->power--->never 2: 设置自动锁屏快捷键: 快捷键设置一般在setting-->devices--->keybo ...
- Object_C 定义全局宏的颜色时,报“Expected identifier”的错误
在定义全局颜色宏的时候,为了整齐把空格删了,写在了同一行里,调用的时候,出错提示“Expected identifier”,如下: 如果宏定义如上那样的话,在调用的时候,会出现如下的问题: 百思不得解 ...
- Educational Codeforces Round 50 (Rated for Div. 2)F. Relatively Prime Powers
实际上就是求在[2,n]中,x != a^b的个数,那么实际上就是要求x=a^b的个数,然后用总数减掉就好了. 直接开方求和显然会有重复的数.容斥搞一下,但实际上是要用到莫比乌斯函数的,另外要注意减掉 ...