简体   繁体   English

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

[英]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? 如何最好地将数组(item => value)对作为GET / POST参数?

In PHP, i can do this: URL: http://localhost/test/testparam.php?a[one]=100&a[two]=200 在PHP中,我可以这样做: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? 有没有办法在ASP.NET MVC中完成相同的工作?

Note: Not sure about Best , but this is what I use. 注意:不确定Best ,但这是我使用的。

You can pass the arguments using the same name for all of them: 您可以使用相同的名称为所有参数传递参数:

For the URL 对于URL

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

You would take the parameters as a string array (or List). 您可以将参数作为字符串数组(或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. 这适用于POST和GET。 For POST you would name the <input> controls all the same name. 对于POST,您可以将<input>控件命名为所有相同的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在AuthorAttribute的ASP.NET Web.API MVC 5的IsAuthorized上获取Post参数 - How to get Post parameters on IsAuthorized of AuthorizeAttribute ASP.NET Web.API MVC 5 如何在ASP.NET MVC中获取不带参数的Action的URL - How to get the URL of the Action without parameters in ASP.NET MVC 如何在ASP.NET MVC中获取所有活动参数(二) - How to get all active parameters in ASP.NET MVC (2) ASP.NET MVC:如何将一组文本框发布到控制器 - ASP.NET MVC: How to post an array of textboxes to a controller 在Asp.net MVC中获取帖子数组 - Getting post array in Asp.net MVC jQuery 后数组 - ASP.Net MVC 4 - jQuery post array - ASP.Net MVC 4 如何在ASP.NET MVC控制器中获取发布JSON数据 - How to get the post json data in asp.net mvc controller 如何在ASP.NET MVC 4 Razor中获取/处理POST数据? - How to get/handle POST data in ASP.NET MVC 4 Razor? 如何使用Razor在ASP.NET MVC中渲染,编辑和发布一组可变的参数? Array []或列表 <string> - How do I use Razor to render, edit, and post an variable set of parameters in ASP.NET MVC? Array[] or List<string> ASP.NET MVC-模型绑定不同实体中具有相同名称的多个参数-GET和POST是否不同? - ASP.NET MVC - Model binding multiple parameters with same name in different entities - GET and POST different?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM