Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take an array of parameters as GET / POST in ASP.NET MVC?

How best to get an array(item=>value) pair as a GET / POST parameter?

In PHP, i can do this: URL: http://localhost/test/testparam.php?a[one]=100&a[two]=200

this gets the parameter as:

Array
(
    [a] => Array
        (
            [one] => 100
            [two] => 200
        )
)

Is there any way to accomplish the same in ASP.NET MVC?

like image 933
Shameem Avatar asked Feb 20 '10 03:02

Shameem


1 Answers

Note: Not sure about Best, but this is what I use.

You can pass the arguments using the same name for all of them:

For the URL

http://localhost/MyController/MyAction?a=hi&a=hello&a=sup

You would take the parameters as a string array (or List).

public ActionResult MyAction(string[] a)
{
     string first = a[0]; // hi
     string second = a[1]; // hello
     string third = a[2]; // sup

     return View();
}

This works for POST and GET. For POST you would name the <input> controls all the same name.

like image 198
Omar Avatar answered Oct 20 '22 01:10

Omar