将数组传递给RouteValues并让它呈现模型binder-friendly url

DMa*_*yer 12 asp.net-mvc-routing asp.net-mvc-3

将一个RouteValueDicitonary或匿名对象传递给@ Url.Action方法(或其任何类似物)时,有没有办法正确传入一个集合对象,或者IEnumerable它会生成一个与默认模型绑定器兼容的URL?

例如,假设我有这样的动作:

public ActionResult Index(ICollection<int> ids)
  {
    ...do something
  }
Run Code Online (Sandbox Code Playgroud)

在我的模板中,我做了这样的事情:

@Url.Action("Index", routeValues:new int[]{1,2,3})
Run Code Online (Sandbox Code Playgroud)

目标是有一个像这样的网址输出:

... /index?ids=1&ids=2&ids=3
Run Code Online (Sandbox Code Playgroud)

但url输出实际上是这样的:

... /index?ids=System.Int[]
Run Code Online (Sandbox Code Playgroud)

我假设目前没有这方面的支持.如果没有,那么在MVC的哪个部分我需要创建一个自定义处理程序或其他什么来覆盖这个默认功能?

Dar*_*rov 11

不幸的是,目前没有现成的帮助程序可以生成这样的URL.所以一种可能性是手动完成:

@(Url.Action("Index") + "?" + string.Join("&", new int[] { 1, 2, 3 }.Select(x => "ids=" + x)))
Run Code Online (Sandbox Code Playgroud)

或编写扩展方法来封装逻辑:

@Url.ActionWithIds("Index", new int[] { 1, 2, 3 })
Run Code Online (Sandbox Code Playgroud)

由于这些是整数,我们不需要url编码,但如果你想对字符串集合做同样的事情,那么值应该正确地进行url编码.