我正在尝试使用从属性中提取的值来填充DropDownList,而我现在的最终结果是除了"System.Web.Mvc.SelectListItem"之外的任何内容.我敢肯定,我在这里省略了一些小步骤,但对于我的生活,我无法弄清楚它是什么.
属性GET生成列表:
public IEnumerable<SelectListItem> AllFoo {
get {
var foo = from g in Bar
orderby g.name
select new SelectListItem {
Value = g.fooid.ToString(),
Text = g.name
};
return foo.AsEnumerable();
}
}
Run Code Online (Sandbox Code Playgroud)
控制器代码:
public ActionResult Edit(string id) {
// n/a code
ViewData["fooList"] = new SelectList(g.AllFoo, g.fooid);
return View(g);
}
Run Code Online (Sandbox Code Playgroud)
视图代码:
<%= Html.DropDownListFor(model => model.fooid, ViewData["fooList"] as SelectList) %>
Run Code Online (Sandbox Code Playgroud)