【发布时间】:2017-04-11 13:45:08
【问题描述】:
我有一个通过 ViewBag 变量填充的下拉列表,我想知道如何使用 id 值设置此列表的选定索引。
这是我创建 ViewBag 变量的地方,注意 id,这是我想用来设置列表中默认选中项的地方:
var events = db.Events.Select(e => new
{
id = e.EventID,
Name = e.EventTitle
}).OrderBy(e => e.Name).ToList();
ViewBag.EventsList = new MultiSelectList(events, "id", "Name");
这是视图中的下拉菜单:
@Html.DropDownList("id", (MultiSelectList)ViewBag.EventsList, new { @class = "form-control", id = "lstTopFeaturedEvent" })
我是否可以使用控制器中设置的 id 属性设置默认选择值?
【问题讨论】:
-
在将模型传递给视图之前,在GET方法中设置属性
id的值(你可以简单地使用ViewBag.EventsList = new SelectList(db.Events, "EventID", "EventTitle");)
标签: asp.net asp.net-mvc asp.net-mvc-5