在 MVC 应用程序中,我有一个显示任务的视图。任务应按分配到的员工进行过滤。员工显示在下拉列表中。
@using (Html.BeginForm("EmployeeDropUsed", "Task", new { id = ?? }))
{
@Html.DropDownList(
"EmployeeDrop", new SelectList(Model.Employees, "Id", "EmployeeName"), "Select employee",
new
{
onchange = @"
var form = document.forms[0];
form.submit();"
})
}
我有一个带有 ActionResult 的 TaskController,从表单中获取数据:
[HttpPost]
public ActionResult EmployeeDropUsed(int id)
{
Employee e = er.GetById(id);
return LoadControls(er.GetAll(), e.Tasks.ToList());
}
ViewModel 看起来像这样
public class TaskModel
{
public TaskModel(List<Employee> employees, List<Task> tasks)
{
Employees = employees;
Tasks = tasks;
}
public List<Task> Tasks { get; set; }
public List<Employee> Employees { get; set; }
}
问题是我不知道如何从下拉列表中获取选定的值到控制器。我尝试在模型中使用 DropDownListFor 和 EmployeeId 属性:
@Html.DropDownListFor(m => Model.EmployeeId, Model.Employees, "Select Employee")
public int EmployeeId { get; set; }
结果是 id 始终为 0。