在 ASP.NET MVC 中为枚举创建下拉列表






4.74/5 (27投票s)
本文档解释了如何在 ASP.NET MVC 中使用枚举类型填充下拉列表。
目录
引言
我将使用两种方法来填充下拉列表。第一种方法比较简单,我使用 SelectListItem
通过枚举填充下拉列表,而在另一种方法中,我创建一个 HTML Helper 方法来填充下拉列表。让我们分别看看。
在 Models 文件夹下创建一个枚举 ActionType
,如以下代码片段所示。
namespace DropdownExample.Models
{
public enum ActionType
{
Create=1,
Read=2,
Update=3,
Delete=4
}
}
方法 1:使用 SelectListItem 和枚举填充下拉列表
我使用 SelectListItem
类型的列表来填充 DropDownList
。因此,我们需要首先从枚举创建一个列表,然后将该列表绑定到视图中 HTML Helper 类的 DropDownListFor
方法。在 Models 文件夹下创建一个模型 (ActionModel
类)。
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace DropdownExample.Models
{
public class ActionModel
{
public ActionModel()
{
ActionsList = new List<SelectListItem>();
}
[Display(Name="Actions")]
public int ActionId { get; set; }
public IEnumerable<SelectListItem> ActionsList { get; set; }
}
}
现在在 Action Controller (ActionController.cs) 中创建一个控制器操作方法,该方法返回一个与模型绑定的视图。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using DropdownExample.Models;
namespace DropdownExample.Controllers
{
public class ActionController : Controller
{
public ActionResult Index()
{
ActionModel model = new ActionModel();
IEnumerable<ActionType> actionTypes = Enum.GetValues(typeof(ActionType))
.Cast<ActionType>();
model.ActionsList = from action in actionTypes
select new SelectListItem
{
Text = action.ToString(),
Value = ((int)action).ToString()
};
return View(model);
}
}
}
在 Views 文件夹下的 Action 子文件夹中创建一个视图 (Index.cshtml)。
@model DropdownExample.Models.ActionModel
@{
ViewBag.Title = "Index";
}
@Html.LabelFor(model=>model.ActionId)
@Html.DropDownListFor(model=>model.ActionId, Model.ActionsList)
运行应用程序,我们将得到如图 1.1 所示的结果。
方法 2:使用枚举的 HTML Helper 方法填充下拉列表
我创建一个 HTML Helper 扩展方法,使用枚举填充下拉列表。在 Models 文件夹下的 Extension
类中创建一个扩展方法 EnumDropDownListFor
。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;
namespace DropdownExample.Models
{
public static class Extension
{
public static MvcHtmlString EnumDropDownListFor<TModel, TProperty, TEnum>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
TEnum selectedValue)
{
IEnumerable<TEnum> values = Enum.GetValues(typeof(TEnum))
.Cast<TEnum>();
IEnumerable<SelectListItem> items = from value in values
select new SelectListItem()
{
Text = value.ToString(),
Value = value.ToString(),
Selected = (value.Equals(selectedValue))
};
return SelectExtensions.DropDownListFor(htmlHelper,expression, items);
}
}
}
在 Models 文件夹下创建一个模型 (ActionTypeModel
类)。
using System.ComponentModel.DataAnnotations;
namespace DropdownExample.Models
{
public class ActionTypeModel
{
[Display(Name = "Actions")]
public int ActionId { get; set; }
public ActionType ActionTypeList { get; set; }
}
}
现在在 Action Controller (ActionController.cs) 中创建一个控制器操作方法 ActionTypes()
,该方法返回一个与模型绑定的视图。
public ActionResult ActionTypes()
{
ActionTypeModel model = new ActionTypeModel();
return View(model);
}
在 Views 文件夹下的 Action 子文件夹中创建一个视图 (ActionTypes.cshtml)。
@model DropdownExample.Models.ActionTypeModel
@using DropdownExample.Models;
@{
ViewBag.Title = "Action Types";
}
@Html.LabelFor(model => model.ActionId)
@Html.EnumDropDownListFor(model => model.ActionId,Model.ActionTypeList)
运行应用程序,我们将得到与图 1.1 相同的结果。
结论
最后,我们创建了一个用于下拉列表的 HTML helper 扩展方法,该方法通过枚举类型填充。