So my problem goes like this,
I have two lists
LIST A contains.
- Item 1
- Item 2
- Item 3
- Item 4
- Item 5
And List B Contains
- Item 1
- Item 2
- Item 3
- Item 4
- .....
- ....
- Item 10
All i want to do is Generate Checkboxes dynamically in MVC Razor View for all the items in B, and of those checkboxes, check(select) all the checkboxes for all the items in A. As A will always be a subset of B.
And then a user can check-uncheck any boxes, and those values can be passed to controller for Saving purposes. The List A will be updated with new values, that user selects.
Any Help ?
UPDATE 1: I am able to get all the items in Model.CheckboxSelections in view. I don't want to use a partial view. I am trying something like following, but something is still missing.
@for (int i = 0; i < Model.CheckboxSelections.Count; i++)
{
@Html.CheckBox(Model.CheckboxSelections[i].Sku.ToString(), Model.CheckboxSelections[i].IsChecked.ToString())
}
With a certain risk of repeating myself I would start my answer with the following sentence:
So:
then a controller:
then a corresponding view (
~/Views/Home/Index.cshtml
):and finally the corresponding editor template which will be automatically rendered for each element of the model collection (
~/Views/Home/EditorTemplates/MyViewModel.cshtml
):and the rendered result (as seen by my Chrome browser) looks like this:
See how easy it is when you use view models?