1

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?

1
  • Can you give us some more of your code. What you are displaying in the view and the relevant code from your action? Commented Nov 11, 2009 at 9:47

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.