我正在尝试创建一个 SelectListItem 列表以在我的 View 中使用。
我尝试使用 Linq 像这样将它传递给 ViewBag:
public ActionResult Create()
{
ViewBag.EmailAccounts = db.EmailAccounts.Select(e => new SelectListItem { e.ID, e.Name });
return View();
}
但它给了我这个错误:
Cannot initialize type 'SelectListItem' with a collection initializer because it does not implement 'System.Collections.IEnumerable'
我是从这里想到这样做的:http://odetocode.com/blogs/scott/archive/2013/03/11/dropdownlistfor-with-asp-net-mvc.aspx
我在这里缺少什么概念?
请您参考如下方法:
您在对象初始值设定项中缺少属性名称:
new SelectListItem { Value = e.ID, Text = e.Name }
