2

I'm using ASP.Net MVC 2, and I've got a slight problem. I need to produce an url from a list of values.

Using the following code:

RouteValueDictionary dic = new RouteValueDictionary();
dic.Add("mylist", new [] { 1, 2, 3 });
return helper.Action(dic["action"] as string, dic["controller"] as string, dic);

I get the url /whatever/System.Int32[] which obviously is not what I am after.

What is the ASP.Net MVC 2 preferred way of generating such an URL? I know the modelbinder can handle lists, but how to generate it?

A quick note on how it is done in MVC 3 would also be appreciated (if the method differs).

David Fox
  • 10,603
  • 9
  • 50
  • 80
Max
  • 4,345
  • 8
  • 38
  • 64
  • How are you expecting your URL to look with this list? – Adam Tuliper Aug 16 '11 at 18:25
  • /whatever?list[0]=1&list[1]=2 would work with the default modelbinder, but prettier for this scenario would probably be list=1&list=2&list=3. In any case, anything that works with both the default model binder and the default URL generator – Max Aug 16 '11 at 18:32
  • possible duplicate of [ASP.NET MVC - Pass array object as a route value within Html.ActionLink(...)](http://stackoverflow.com/questions/717690/asp-net-mvc-pass-array-object-as-a-route-value-within-html-actionlink) – dav_i Feb 14 '14 at 17:23

2 Answers2

1

See if this prior response helps you (mvc 3 would be the same - its the same routing engine as part of asp.net 4)

ASP.NET MVC - Pass array object as a route value within Html.ActionLink(...)

Community
  • 1
  • 1
Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
0

You could always make your own List class with a ToString() override that prints how you would like it. That seems like the simplest (but perhaps the most 'hackish') way to do this.

public MyRouteList<T> : List<T>
{
     public override string ToString()
     {
          return string.Join("&", this.Select(x => "list=" + x.ToString()));
     }
}
Tejs
  • 40,736
  • 10
  • 68
  • 86