In an MVC application, I have a view showing tasks. Tasks should be filtered by the staff to whom they are assigned. Employees are displayed in the drop-down list.
@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();"
})
}
I have a TaskController with ActionResult, I take data from the form:
[HttpPost]
public ActionResult EmployeeDropUsed(int id)
{
Employee e = er.GetById(id);
return LoadControls(er.GetAll(), e.Tasks.ToList());
}
ViewModel looks like this:
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; }
}
The problem is that I do not know how to get the selected value from returning to the controller. I tried using the DropDownListFor and EmployeeId property in the model:
@Html.DropDownListFor(m => Model.EmployeeId, Model.Employees, "Select Employee")
public int EmployeeId { get; set; }
As a result, id is always 0.
source
share