views:

32

answers:

1

I have a view for editing department details. In this view, all employees are listed and user selects which employees work in this department. Is there a way to submit only the selected employees as department.Employees (without using javascript)?

public ActionResult(Department department)
{
    Save(department); // department.Employees should only contain checked employees
}
A: 

You cant really choose what you 'submit' from the page without javascript to manipulate the form, but why do you need to do it like that?

If you just selecting employees from a list, you could just loop around all the employees in the model and create a check box for them (you may have to manually render as below - Html.Checkbox doesnt seem to work for multiple check boxes of same name)

<input type="checkbox" value="<%= Html.Encode(employee.Id)%>" name="employeeForDepartment" />

Then in your action you can do something like this:

public ActionResult Bla(int departmentId, int[] employeeForDepartment) .....

assuming that the employee id is an int. Then you can process that list accordingly (only checked employees will be submitted). It doesnt have the model binding you were using previously, but in this case, it doesnt really lend its self to processing in that way.

RM
I want it for simplicity. Actually, the solution I am currently using is similar to yours, but a little bit more clean. I just want to know if there is a much simpler solution, without manual touches like traversing arrays, etc.
Serhat Özgel