.net MVC 中枚举类型Enum 转化成 下拉列表的数据源

时间:2021-09-06 14:53:37

第一次写技术博文,记录下工作中遇到的问题,给自己的知识做个备份,也希望能帮助到其他的同学

最近接手了公司的一个新的项目。有个页面涉及相关设计。 分享一个经常用到的吧。

 

方法一:

直入主题吧

我们的目的是把 Enum类型里面的

Enum:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AirExpress.Core
{
    public enum PackageExceptionTypeEnums
    {
        [Display(Name = "破损")]
        Damaged = 1,

        [Display(Name = "违禁品")]
        ContrabandGoods = 2,

        [Display(Name = "超件")]
        BeyondPackages = 3
    }
}

 

实现效果:

.net MVC 中枚举类型Enum 转化成 下拉列表的数据源

拓展类:

public static class EnumExtention
    {

        public static SelectList ToSelectList<T>(int? selectValue = null,string AttributeName ="Name")
        {
            IList<SelectListItem> selectItemList = new List<SelectListItem>();
            Type enumType = typeof(T);
            Type attrType = typeof(DisplayAttribute);
            foreach (int value in Enum.GetValues(typeof(T)))
            {
                SelectListItem item = new SelectListItem();

                string name = Enum.GetName(enumType, value);
                object[] objAttrs = enumType.GetField(name).GetCustomAttributes(attrType, true);
                if(objAttrs!=null && objAttrs[0] is DisplayAttribute)
                {
                    DisplayAttribute descAttr = objAttrs[0] as DisplayAttribute;
                    PropertyInfo propertyName = attrType.GetProperty(AttributeName);
                    item.Text = propertyName.GetValue(descAttr).ToString();
                }else
                {
                    item.Text = Enum.GetName(enumType, value);
                }
                    
                item.Value = value.ToString();
                item.Selected = value == selectValue ? true : false;
                selectItemList.Add(item);
            }

            SelectList selectList = new SelectList(selectItemList, "Value", "Text", selectValue.Value);
            return selectList;
        }
    }

 

Attribute 类

public  class DisplayAttribute : Attribute
    {

        public string Name { get; set; }
        public string FullName { get; set; }

    }

 

 

Controller:

ViewBag.IndexexceptionSelectedList = EnumsExtension.ToSelectList<PackageExceptionTypeEnums>(input.ExceptionType);

 

View:

@Html.DropDownListFor(m => m.ExceptionType, ViewBag.exceptionSelectedList as SelectList, "----请选择----")

 

方法二:

。net MVC  里其实也自带了相应的 下拉列表拓展

EnumDropDownListFor

 后来我找资料的时候无意中看到了。 那就不用自己再去实现一遍了

 

让我们先看下MVC自带的源码文件:

 public static class SelectExtensions
    {
      
        public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name);
        
        public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList);
       
        public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, string optionLabel);
        
        public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, IDictionary<string, object> htmlAttributes);
        
        public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, object htmlAttributes);
        
        public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, string optionLabel);
        
        public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, string optionLabel, IDictionary<string, object> htmlAttributes);
        
        public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, string optionLabel, object htmlAttributes);
       
        public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList);
        
        public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, IDictionary<string, object> htmlAttributes);
       
        public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, object htmlAttributes);
       
        public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel);
       
        public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel, IDictionary<string, object> htmlAttributes);
        
        public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel, object htmlAttributes);
       
        public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression);
        
        public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, IDictionary<string, object> htmlAttributes);
       
        public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, object htmlAttributes);
       
        public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, string optionLabel);
       
        public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, string optionLabel, IDictionary<string, object> htmlAttributes);
        
        public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, string optionLabel, object htmlAttributes);
       
        public static MvcHtmlString ListBox(this HtmlHelper htmlHelper, string name);
        
        public static MvcHtmlString ListBox(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList);
        
        public static MvcHtmlString ListBox(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, IDictionary<string, object> htmlAttributes);
       
        public static MvcHtmlString ListBox(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, object htmlAttributes);
        
        public static MvcHtmlString ListBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList);
        
        public static MvcHtmlString ListBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, IDictionary<string, object> htmlAttributes);
        
        public static MvcHtmlString ListBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, object htmlAttributes);
    }

 

 

view:

//x.TakeoOffPeriod 是枚举类型

  @Html.EnumDropDownListFor(x => x.TakeoffPeriod, new AirExpress.Core.TakeoffPeriod())

Enum:

  public enum TakeoffPeriod
    {
        [Display(Name = "全天")]
        Allday = 1,
        [Display(Name = "早班6:00-10:00")]
        Morning = 2,
        [Display(Name = "午班10:00-18:00")]
        Noon = 3,
        [Display(Name = "晚班18:00-次日6:00")]
        Night =4
    }

.net MVC 中枚举类型Enum 转化成 下拉列表的数据源