tags:

views:

160

answers:

1

Can anyone advise me how to write an action method that handles data from a form with the following input elements?
<input type="checkbox" name="check[1]" />
<input type="checkbox" name="check[3]" />
The numbers in square brackets are not array indexes but can be IDs e.g. Also, I do not know how many such input fields the form is going to have. Or is there a better of passing such data from a form?

+2  A: 
public ActionResult Foo(IEnumerable<string> check)
{

...then put the ID in the value attribute of the input. You'll get a list of the IDs of the checked inputs.

You can change IEnumerable to IEnumerable or IEnumerable if your IDs happen to be in that format. The MVC framework will convert it for you.

Craig Stuntz