使用MVC将列表用作查询字符串参数

问题描述 投票:4回答:3

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

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

我可以通过某种方式轻松完成此操作吗?

.net list asp.net-mvc-2 query-string
3个回答
2
投票

1
投票
怎么样:

0
投票
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 ""; } }
© www.soinside.com 2019 - 2024. All rights reserved.