IEnumerable<;选择列表项>;未绑定到枚举
本文关键字:绑定 gt 枚举 列表 lt 选择 IEnumerable | 更新日期: 2023-09-27 18:23:47
我正在尝试使用enum
作为DropDownList
的文本和值的来源。不幸的是,它没有按预期工作-DropDownList已经填充,但没有选择任何选项
这是枚举:
public enum OrderLineState
{
Draft = 0,
InProcess = 1,
Done = 2
}
以下是我如何从枚举填充DropDown:
public IEnumerable<Helper.OrderLineStateData> GetOrderLineStates()
{
return from Enums.OrderLineState s in Enum.GetValues(typeof(Enums.OrderLineState))
select new Helper.OrderLineStateData() { OrderLineStateId = (int)s, OrderLineStateName = s.ToString() };
}
这是控制器:
[Authorize]
public async Task<ActionResult> EditOrderLine(int id)
{
ViewBag.OrderLineStates = new SelectList(_olr.GetOrderLineStates(), "OrderLineStateId", "OrderLineStateName");
var result = await _olr.GetAllOrderLines(id);
return PartialView(result);
}
最后-Razor视图:
<td>
@Html.DropDownListFor(x => item.LineState, (IEnumerable<SelectListItem>)ViewBag.OrderLineStates, new { @class = "form-control" })
</td>
如果您是MVC 5,则有一个枚举帮助程序。试试这个:
public enum OrderLineState
{
[Display(Name = "Draft")]
Draft = 0,
[Display(Name = "In Process")]
InProcess = 1,
[Display(Name = "Done")]
Done = 2
}
然后
@Html.EnumDropDownListFor(model => model.LineState)
http://www.drdobbs.com/windows/aspnet-mvc-51/240168353