Search code examples
asp.net-mvc-3asp.net-mvc-routing

Passing an array to RouteValues and have it render model binder-friendly url


When passing a RouteValueDicitonary or anonymous object into a @Url.Action method (or any of its analogs), is there a way to properly pass in a collection object or IEnumerable so that it would generate a url that would be compatible with the default model binder?

For example, say I have an action like this:

public ActionResult Index(ICollection<int> ids)
  {
    ...do something
  }

and in my template I do something like this:

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

the goal is to have a url output like this:

... /index?ids=1&ids=2&ids=3

but the url output is actually something like this:

... /index?ids=System.Int[]

I'm assuming there is no support for this currently. If not, then at what part in MVC do I need to create a custom handler or whatever to override this default functionality?


Solution

  • Unfortunately currently there is no existing helper that will allow you to generate such url. So one possibility is to do it manually:

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

    or write an extension method to encapsulate the logic:

    @Url.ActionWithIds("Index", new int[] { 1, 2, 3 })
    

    Since those are integers we don't need url encoding but if you want to do the same thing with a collection of strings the values should be properly url encoded.