@Html.EnumDropDownListFor( x => x.YourEnumField, "Select My Type", new { @class = "form-control" })
对于MVC v5,请使用EnumHelper
@Html.DropDownList("MyType", EnumHelper.GetSelectList(typeof(MyType)) , "Select My Type", new { @class = "form-control" })
对于MVC 5及更低版本
我将符文的答案扩展为扩展方法:
namespace MyApp.Common{ public static class MyExtensions{ public static SelectList ToSelectList<TEnum>(this TEnum enumObj) where TEnum : struct, IComparable, IFormattable, IConvertible { var values = from TEnum e in Enum.GetValues(typeof(TEnum)) select new { Id = e, Name = e.ToString() }; return new SelectList(values, "Id", "Name", enumObj); } }}
这使您可以编写:
ViewData["taskStatus"] = task.Status.ToSelectList();
通过
using MyApp.Common
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)