第一次寫技術博文,記錄下工作中遇到的問題,給自己的知識做個備份,也希望能幫助到其他的同學
最近接手了公司的一個新的項目。有個頁面涉及相關設計。 分享一個經常用到的吧。
方法一:
直入主題吧
我們的目的是把 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 }