Reputation: 21
Hi i've a problem related to html.checkbox in my MVC application.
My scenario is:
I've a list(index view) page where i bind data from the database with a checkbox to select/deselect the item. when i click save button i want to get selected rows to save those items back to db.
i used
1. <input type="checkbox" id="chk2" value="<%= item.recid %>" >
// I'm not getting value of chk2 in serverside
2. <%= html.CheckBox("chk1")%>
// i'm getting chk1 in serverside with value like 'true,false,true,false...'
in my model view iteration.
So how to do that in MVC application?
Upvotes: 1
Views: 3094
Reputation: 583
This is how I do it...
In the view, give all your checkboxes the same name and a unique value.
<input type="checkbox" name="MyCheckboxes" value="<%= item.recid %>" >
In your controller action method, pass an IList with the name of the checkboxes.
public ActionResult MyActionMethod(IList<string> MyCheckboxes)
{
...
}
You'll receive in MyCheckboxes a list of the values of only those checkboxes that were selected.
Upvotes: 7
Reputation: 1478
For 1), you need to specify a name on the input element.
You then need to match that name to a parameter on your Action Method.
Upvotes: 1