如何在ASP.NET MVC中将参数数组作为GET/POST?

Sha*_*eem 7 c# parameters asp.net-mvc http

如何最好地将数组(item => value)对作为GET/POST参数?

在PHP中,我可以这样做:URL:http://localhost/test/testparam.php?a [one] = 100&a [two] = 200

这将获取参数:

Array
(
    [a] => Array
        (
            [one] => 100
            [two] => 200
        )
)
Run Code Online (Sandbox Code Playgroud)

有没有办法在ASP.NET MVC中完成相同的工作?

Oma*_*mar 8

注意:不确定Best,但这是我使用的.

您可以使用相同的名称为所有参数传递参数:

对于URL

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

您可以将参数作为字符串数组(或List).

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

     return View();
}
Run Code Online (Sandbox Code Playgroud)

这适用于POST和GET.对于POST,您可以将<input>控件命名为同名.