MVC SelectList selectValue无法正常工作。为什么?

时间:2018-07-05 09:22:56

标签: c# asp.net-mvc model-view-controller dropdownlistfor

viewModel.CategoryList = new SelectList(db.Categories, "ID", "Name", product.CategoryID);

@Html.DropDownListFor(vm => vm.CategoryID, Model.CategoryList, htmlAttributes: new {@class = "form-control" })

"ID" == product.categoryID

处并没有真正显示结果

1 个答案:

答案 0 :(得分:0)

尝试

在控制器上

viewBag.Categories = new SelectList(db.Categories,“ ID”,“ Name”,product.CategoryID);

在视图上

@ Html.DropDownListFor(vm => vm.CategoryID,(selectlist)viewBag.Categories,htmlAttributes:new {@class =“ form-control”})