sudhir
sudhir

Reputation: 21

How to get selected rows using checkbox in asp.net MVC

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

Answers (2)

Ozzie Perez
Ozzie Perez

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

Christopher Stott
Christopher Stott

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

Related Questions