How to take an array of parameters as GET / POST i

2020-07-10 05:41发布

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?

1条回答
我想做一个坏孩纸
2楼-- · 2020-07-10 06:27

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.

查看更多
登录 后发表回答