这里我试图创建动态下拉列表,从View Viewodeode of View
加载项目将View模型传递给MVC View
public ActionResult Create()
{
OrgVM model = new OrgVM();
model.regions = getRegions();
return View(model);
}
private List<Region> getRegions()
{
var list = new List<Region>();
list = context.Set<Region>().ToList().Select(s => new Region
{
Id = s.RegionId,
Name = s.Name,
}).ToList();
return list;
}
现在在Create View中调用View模型,如下所示
@model xyz.ViewModels.OrgVM
正在生成下拉列表
@Html.DropDownListFor(x => Model.regions, new SelectList(Model.regions), htmlAttributes: new { @class = "form-control" })
答案 0 :(得分:0)
用很少的R和D解决这个问题
@Html.DropDownListFor(m => Model.regions,Model.regions.Select(d => new
SelectListItem()
{
Value = d.RegionId.ToString(),
Text = d.Name.ToString()
}))