程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> .net MVC 中枚舉類型Enum 轉化成 下拉列表的數據源,mvcenum

.net MVC 中枚舉類型Enum 轉化成 下拉列表的數據源,mvcenum

編輯:關於.NET

.net MVC 中枚舉類型Enum 轉化成 下拉列表的數據源,mvcenum


第一次寫技術博文,記錄下工作中遇到的問題,給自己的知識做個備份,也希望能幫助到其他的同學

最近接手了公司的一個新的項目。有個頁面涉及相關設計。 分享一個經常用到的吧。

 

方法一:

直入主題吧

我們的目的是把 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
    }
}

 

實現效果:

拓展類:

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
    }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved