C# 基于asp.net mvc5中数据库的值设置dropdownlist的值
我在我的控制器中声明了一个SelectListItem对象,我可以在Create()页面中将它们填充到我的DropDownList中,但是我在尝试从模型中获取值以在Edit()页面中设置DropDownList的选定值时遇到了问题 控制器中Create()的代码:C# 基于asp.net mvc5中数据库的值设置dropdownlist的值,c#,asp.net-mvc,html.dropdownlistfor,populate,selectedvalue,C#,Asp.net Mvc,Html.dropdownlistfor,Populate,Selectedvalue,我在我的控制器中声明了一个SelectListItem对象,我可以在Create()页面中将它们填充到我的DropDownList中,但是我在尝试从模型中获取值以在Edit()页面中设置DropDownList的选定值时遇到了问题 控制器中Create()的代码: public ActionResult Create() { var tagList = new List<SelectListItem>(); tagList.Add(new Se
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();
}
public ActionResult Create()
{
var tagList=新列表();
添加(新的SelectListItem(){Text=“Classic”,Value=“Classic”});
添加(新的SelectListItem(){Text=“Promo”,Value=“Promo”});
添加(新的SelectListItem(){Text=“Limited”,Value=“Limited”});
添加(新的SelectListItem(){Text=“Classic”,Value=“Classic”});
tagList.Add(new SelectListItem(){Text=“new”,Value=“new”});
var catList=新列表();
添加(新的SelectListItem(){Text=“Men”,Value=“Men”});
添加(新的SelectListItem(){Text=“Women”,Value=“Women”});
添加(新建SelectListItem(){Text=“Sport”,Value=“Sport”});
添加(新的SelectListItem(){Text=“Casual”,Value=“Casual”});
var statusList=新列表();
statusList.Add(新建SelectListItem(){Text=“Available”,Value=“Available”});
statusList.Add(新建SelectListItem(){Text=“Unavailable”,Value=“Unavailable”});
ViewBag.tagDropDown=标记列表;
ViewBag.catDropDown=catList;
ViewBag.statusDropDown=状态列表;
返回视图();
}
我可以使用所有Viewbag在Create()视图页面中填充DropDownList
但是现在我希望在编辑()视图页面中填充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>
@LabelFor(model=>model.category,htmlAttributes:new{@class=“controllabel col-md-2”})
@DropDownListFor(model=>model.category,new SelectList(ViewBag.catDropDown,“value”,“text”),htmlAttributes:new{@class=“form control”})
只需在编辑操作方法中设置视图模型对象的类别
属性值
public ActionResult Edit(int id)
{
var vm=new YourViewModel();
vm.category="Sport"; // Replace this hard coded value with value from db
// to do : Load ViewBag.catDropDown
return View(vm);
}
现在,假设您的视图强类型为YourViewModel