我有一个List,我想在下拉框中填充此列表。
FROM CONTROLLER:
public ActionResult Index()
{
List<SelectListItem> hum = allHuman();
return View();
}
FROM VIEW:
@Html.DropDownListFor( ..... // NOW HOW CAN I DISPLAY THE VALUES IN THE DROPDOWNLIST ?
答案 0 :(得分:0)
控制器
var list = new SelectList(allHuman(), "Id", "Name");
视图
@Html.DropDownList("humans", Model as SelectList)
答案 1 :(得分:0)
您应首先定义模型类型。
class MyViewModel
{
// I assume you will use this property to read the selected value
public string Human {get;set;}
//dropdown values
public List<SelectListItem> AllHumans {get;set;}
}
然后在控制器中创建一个实例:
public ActionResult Index()
{
var model = new MyViewModel { AllHumans = allHuman()};
return View(model);
}
然后您可以在视图中显示它:
@model MyViewModel
@Html.DropDownListFor(model => model.Human, Model.AllHumans, null)