8

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!

2 Answers 2

15

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

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

3 Comments

Darin, thanks for the quick response, "one shot, one hit", as usual:)
Thanks for your answer Mr. @Darin Dimitrov, Very Helpful !
Is it possible for multidimensional array?
12

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) {
   ...
}

4 Comments

Courtesy of link
I was looking for Model binder solution, anyway this is a nice trick too.
Right on. I wanted to get this up for people looking for a quick and dirty solution.
@marcus for me Request.Form.GetValues("my_value") returns null but Request.Form.GetValues("my_value[]") returns the array of values, so it seems you need to keep the square brackets. +1 anways

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.