是否可以将ViewData["MenuItem.StoreMenuId"]
转换为ViewBag?我使用ViewBags存储IEnumerable<SelectListItem>
,因此我可以在视图中绑定到@Html.DropdownListFor
。如果它是一个非嵌套的简单属性(即StoreMenuId),则可以正常工作:
ViewBag:
ViewBag.StoreMenuId = new SelectList(_db.StoreMenus.Where(m => m.Store.Id == model.StoreId), "Id", "Name");
DropDownListFor:
@Html.DropDownListFor(model => model.StoreMenuId, null, "", new { @class = "form-control" })
但是如果属性是嵌套的(即MenuItem.StoreMenuId)怎么办? 这不起作用:
ViewBag:
ViewBag.MenuItem.StoreMenuId = new SelectList(_db.StoreMenus.Where(m => m.Store.Id == model.StoreId), "Id", "Name");
DropDownListFor:
@Html.DropDownListFor(model => model.MenuItem.StoreMenuId, null, "", new { @class = "form-control" })
最终工作的目的是将ViewBag.MenuItem.StoreMenuId
更改为ViewData["MenuItem.StoreMenuId"]
此作品:
的ViewData:
ViewData["MenuItem.StoreMenuId"] = new SelectList(_db.StoreMenus.Where(m => m.Store.Id == model.StoreId), "Id", "Name");
DropDownListFor:
@Html.DropDownListFor(model => model.MenuItem.StoreMenuId, null, "", new { @class = "form-control" })