Dave Bish
Dave Bish

Reputation: 19646

Multi-value GET parameters - route generation

When using multi-value GET parameters like so:

/?param=1&param=2&param=3

You can automatically model-bind to a list, like so:

public ActionResult MyAction(List<int> param)

How can I pass such values using anonymous types, in URL generation?

@Url.Action("MyAction", new { param = ?? })

Using an array / list doesn't work -

@Url.Action("MyAction", new { param = new List<string>{ "1", "2", "3" } })

As it just spits out Object.ToString() like:

?param=System.Collections.Generic.List%601%5BSystem.String%5D

Cheers

Upvotes: 2

Views: 278

Answers (1)

Naz
Naz

Reputation: 1803

Not the nice solution your after, buy you could do, assuming you know whether to add the ? or not.

@Url.Action("MyAction")[email protected]("&param=", new [] {1, 2, 3})

Maybe a helper could be written that does this. see URL.Action with a string array?

Upvotes: 1

Related Questions