I have a list of items and I would like to delete items that are checked in a list of checkboxes.
I can't use something like CheckboxList
since I'm using Grid.Mvc
to display my lines. That is why I create checkboxes in each line with column.add("<input type="checkbox".....>);
.
Every checkbox has its own ID:
<input type="checkbox" id="3">
<input type="checkbox" id="4">...
I would like to know how to pass all checked checkbox IDs to the controller (from there I will perform delete operations). How can I post an array of checked IDs from my form to my controller action with one button press?
If you want generated html like
Then you can use the following code
It won't pass selectedItems to controller.
Example of generated HTML:
Controller action:
Note that the checkboxes do not have an
id
attribute. It is not used for model binding. Instead it has aname
attribute named "deletedItems" that matches the name of the argument of theMyAction
controller action, and that is what is used when model binding. Thevalue
attribute of checked checkboxes will be used to populate thedeletedItems
array ofint[]
.