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?
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:Then you could generate your own query string like this:
This needs some polish as you should UrlEncode your strings and it creates a trailing “&”, but this should give you the basic idea.