0
我已經在我的控制器中聲明瞭一個SelectListItem對象,我可以將它們填充到Create()頁面的DropDownList中,但是我嘗試獲取值從模型中在Edit()頁面中設置DropDownList的選定值。創建的基於asp.net中數據庫的值設置dropdownlist的值mvc5
代碼()的控制器:
public ActionResult Create()
{
var tagList = new List<SelectListItem>();
tagList.Add(new SelectListItem() { Text = "Classic", Value = "Classic" });
tagList.Add(new SelectListItem() { Text = "Promo", Value = "Promo" });
tagList.Add(new SelectListItem() { Text = "Limited", Value = "Limited" });
tagList.Add(new SelectListItem() { Text = "Classic", Value = "Classic" });
tagList.Add(new SelectListItem() { Text = "New", Value = "New" });
var catList = new List<SelectListItem>();
catList.Add(new SelectListItem() { Text = "Men", Value = "Men" });
catList.Add(new SelectListItem() { Text = "Women", Value = "Women" });
catList.Add(new SelectListItem() { Text = "Sport", Value = "Sport" });
catList.Add(new SelectListItem() { Text = "Casual", Value = "Casual" });
var statusList = new List<SelectListItem>();
statusList.Add(new SelectListItem() { Text = "Available", Value = "Available" });
statusList.Add(new SelectListItem() { Text = "Unavailable", Value = "Unavailable" });
ViewBag.tagDropDown = tagList;
ViewBag.catDropDown = catList;
ViewBag.statusDropDown = statusList;
return View();
}
我能夠填充的DropDownList中的Create()使用所有Viewbag(S)視圖頁。
但是現在我希望在Edit()視圖頁面中填充DropDownList,同時從模型中設置選定的值。
以下是編輯()查看頁面代碼:
<div class="form-group">
@Html.LabelFor(model => model.category, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.category, new SelectList(ViewBag.catDropDown, "value", "text"), htmlAttributes: new { @class = "form-control" })
</div>
</div>