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();
        }



代码示例,点击下载


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安得权

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值