使用 MVC 使用 List 作为查询字符串参数

发布于 2024-09-16 10:58:21 字数 534 浏览 1 评论 0原文

我有一个如下所示的操作方法:

public ActionResult DoSomething(string par, IEnumerable<string> mystrings)

我想使用 Url.Action 将其映射到 URL,并在 RouteValueDictionary 中传递 mystrings。但是,这只会产生一个仅对应于 mystrings.ToString() 的查询字符串。

如何在查询字符串中传递列表? MVC 2 中是否有一些功能支持此功能?

澄清:操作方法是使用 GET 调用的,而不是 POST。

我的操作方法解析查询字符串 DoSomething?mystrings=aaa&mystrings=bbb 没有问题

但是,我无法使用 Url.Action 生成它。传递列表会生成以下查询字符串: mystrings=system.collections.generic.list%601%5bsystem.string%5d

有什么方法可以轻松完成此操作吗?

I have an action method that looks like this:

public ActionResult DoSomething(string par, IEnumerable<string> mystrings)

I wanted to map this to a URL using Url.Action, passing mystrings in the RouteValueDictionary. However, this only yields a query string that only corresponds to mystrings.ToString().

How could I pass a list in the query string? Is there some functionality in MVC 2 that supports this?

CLARIFICATION: The action method is called using GET, not POST.

There is no problem for my action method to parse the query string DoSomething?mystrings=aaa&mystrings=bbb

However, I cannot generate this using Url.Action. Passing a list generates the following query string: mystrings=system.collections.generic.list%601%5bsystem.string%5d

Is there some way I could accomplish this easily?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

2024-09-23 10:58:21

是的。 模型绑定到列表

编辑:好的,现在我明白你要做什么了。我不认为 ASP.NET MVC 具有内置功能,因为它旨在从具有唯一名称的路由值生成查询字符串。你可能必须自己动手。我将在 IEnumerable 上创建一个扩展方法,如下所示:

public static class Extensions
{
    public static string ToQueryString(this IEnumerable<string> items)
    {
        return items.Aggregate("", (curr, next) => curr + "mystring=" + next + "&");
    }
}

然后您可以生成自己的查询字符串,如下所示:

<%= Url.Action("DoSomething?" + Model.Data.ToQueryString()) %>

这需要一些改进,因为您应该对字符串进行 UrlEncode,它会创建一个尾随的“&”。 ”,但这应该给你基本的想法。

Yes. Model Binding To A List

EDIT: Ok, now I see where you're going with this. I don't think ASP.NET MVC has that built-in since it's designed to generate query strings from route values that have unique names. You may have to roll your own. I would create an extension method on IEnumerable<String> like this:

public static class Extensions
{
    public static string ToQueryString(this IEnumerable<string> items)
    {
        return items.Aggregate("", (curr, next) => curr + "mystring=" + next + "&");
    }
}

Then you could generate your own query string like this:

<%= Url.Action("DoSomething?" + Model.Data.ToQueryString()) %>

This needs some polish as you should UrlEncode your strings and it creates a trailing "&", but this should give you the basic idea.

空城旧梦 2024-09-23 10:58:21

怎么样:

<%: Html.ActionLink("foo", "DoSomething", new RouteValueDictionary() { 
    { "mystrings[0]", "aaa" }, { "mystrings[1]", "bbb" } 
}) %>

生成:

<a href="/Home/DoSomething?mystrings%5B0%5D=aaa&mystrings%5B1%5D=bbb">foo</a>

这不完全是您正在寻找的 URL,但它将成功绑定到您的控制器操作。如果您想生成不带方括号的 url,则需要使用自己的辅助方法。

How about:

<%: Html.ActionLink("foo", "DoSomething", new RouteValueDictionary() { 
    { "mystrings[0]", "aaa" }, { "mystrings[1]", "bbb" } 
}) %>

which generates:

<a href="/Home/DoSomething?mystrings%5B0%5D=aaa&mystrings%5B1%5D=bbb">foo</a>

This is not exactly the URL you were looking for but it will successfully bind to your controller action. If you want to generate an url without the square brackets you will need to roll your own helper method.

给不了的爱 2024-09-23 10:58:21
public static class Extensions
{
  public static string ToQueryString(this IEnumerable<string> items)
  {
     if (items.Count>0)
     {
          var urlParam = string.Join("&", items.ToArray());
          return "?"+ urlParam;
     }
     return "";
  }
}
public static class Extensions
{
  public static string ToQueryString(this IEnumerable<string> items)
  {
     if (items.Count>0)
     {
          var urlParam = string.Join("&", items.ToArray());
          return "?"+ urlParam;
     }
     return "";
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文