①.在Action中查询要绑定到DropDownListFor的数据,并赋值给ViewBag:
public ActionResult Create()
{
ViewBag.Categories = new SelectList(db.Category, "Id", "Name");
return View();
} ②.在View中将数据绑定到DropDownListFor:
@Html.DropDownListFor(model => model.CategoryId, ViewBag.Categories as IEnumerable<SelectListItem>);
2.DropDownListFor选中指定项
这需要在SelectList构造函数中设置,例如:
public ActionResult Create()
{
ViewBag.Categories = new SelectList(db.Category, "Id", "Name", "2");
return View();
}
评论(0)
暂无评论。