(This is for MVC 4 i'm not sure if anything's changed)
This is pretty easy to do if you're using strongly typed views.
The auto generated "List" type on strongly typed views is similar to that layout.
Assuming this view takes a @model List<EmployeeViewModel>
. Create a strongly typed partial view with a CheckBoxViewModel
class to use for each employee checkbox property, that has an entry for the label and id of the checkbox with a bool for its checked-ness.
You can then use Html.EditorFor
with the CheckBoxViewModel
s on the EmployeeViewModel
to display this partial view. The partial view would be something along the lines of...
@model CheckBoxViewModel
@Html.CheckBoxFor(cbox => cbox.Checked)
@Html.LabelFor(cbox => cbox.Checked, Model.Label)
@Html.HiddenFor(cbox => cbox.Id)
@Html.HiddenFor(cbox => cbox.Label)
You might want to miss off the LabelFor
looking at your example, but as long as the hidden one is there I think that's fine.
A POST
version of the view's controller can then be given an input parameter type of List<EmployeeViewModel> employees
which will contain the checkbox values inside the employee objects.
For the tickbox on the left, you can just create a straight HTML version with a plain JavaScript or jQuery event to check the ones in the same row when it is checked/unchecked. Something like that is detailed here - check uncheck All checkboxes with another single checkbox use jquery