artvolk
artvolk

Reputation: 9518

Post array in ASP.NET MVC 2 using name="array_name[]" for form elements

Good day!

In PHP it is possible to assign name attribute to input elements with square brackets, like this: name="my_value[]" and PHP automagically converts this to array on server side.

Is this possible in ASP.NET MVC? If not is there any alternative to process a bunch of checkboxes in ASP.NET MVC?

Thanks in advance!

Upvotes: 8

Views: 18819

Answers (2)

marcus
marcus

Reputation: 151

Make sure the name is still the same, but go ahead and remove the brackets. You can then add the values to an array like so:

string[] values = Request.Form.GetValues("my_value");
foreach (string value in values) {
   ...
}

Upvotes: 12

Darin Dimitrov
Darin Dimitrov

Reputation: 1038820

Yes, it is possible. You might take a look at the following blog post about the convention used by the default model binder.

Upvotes: 15

Related Questions