【问题标题】:How to take an array of parameters as GET / POST in ASP.NET MVC?如何在 ASP.NET MVC 中将一组参数作为 GET / POST?
【发布时间】:2010-02-20 03:09:32
【问题描述】:

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

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

这得到的参数为:

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

有没有办法在 ASP.NET MVC 中实现同样的功能?

【问题讨论】:

    标签: c# asp.net-mvc http parameters


    【解决方案1】:

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

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

    对于网址

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

    您可以将参数作为字符串数组(或列表)。

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

    这适用于 POST 和 GET。对于 POST,您可以将 <input> 控件命名为所有相同的名称。

    【讨论】:

    • 这看起来不错。但是,是否可以使用 Html.RouteLink 生成这种 URL?
    • 在 ASP.NET MVC 中是否有此类参数的名称? (有助于在谷歌上搜索更多相关信息)
    • @sleepy - 我尝试让Html.RouteLink 使用这种 URL,但是当将参数作为匿名对象提供时,属性不能具有确切的名称。我认为您最好的选择是构建一个输出正确 URL 的自定义 HtmlHelper。
    猜你喜欢
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多