建立模型
public class MyViewModel
{
public int EmployeeId { get; set; }
public string Comments { get; set; }
public List<people> peopleList {get; set; }
}
public class people
{
public int Id { get; set; }
public string FullName { get; set; }
}
controller
public IActionResult Index()
{
var vm = new MyViewModel();
vm.peopleList = new List<people>
{
new people{ Id = 1, FullName = "smit" },
new people{ Id = 2, FullName = "delly" }
};
return View(vm);
}
view
@model MyViewModel
<form asp-controller="Home" asp-action="index">
<select asp-for="EmployeeId"
asp-items="@(new SelectList(Model.peopleList ,"Id","FullName"))">
<option>Please select one</option>
</select>
<input type="submit"/>
</form>
“Id”,"FullName"会覆盖value和text,类似于
new SelectList(){
Text="",
value=""
}