It's a fairly common requirement to want to generate a url which handles collections in a way that is compatible with the default model binder, e.g. see:
http://stackoverflow.com/questions/717690/asp-net-mvc-pass-array-object-as-a-route-value-within-html-actionlink
http://stackoverflow.com/questions/8391055/passing-an-array-to-routevalues-and-have-it-render-model-binder-friendly-url
Basically this would involve special-casing IEnumerable values in a RouteValueDictionary, and generating repeated keys. For example:
routeValues.Add("productId", new[] {1, 3, 5});
would result in a Url being generated something like:
http://server/Controller/Action?productId=1&productId=3&productId=5
As far as I can see with Reflector, the generation of the Url is currently delegated to System.Web.Routing.ParsedRoute.Bind(), which is a Framework class and isn't going to change any time soon.
But this could be achieved in the MVC framework by modifying Url.GenerateUrl as follows:
- Clone the "routeValues" dictionary, and remove all elements whose value is IEnumerable. Pass the cloned dictionary to "routeCollection.GetVirtualPathForArea(requestContext, routeName, values)", which will generate a Url that excludes the IEnumerable routeValues.
- Append the IEnumerable routeValues as queryString parameters.
By making this change in UrlHelper.GenerateUrl, it would automatically become available for all helpers that use UrlHelper.GenerateUrl (UrlHelper.Action, LinkExtensions.ActionLink, etc).
http://stackoverflow.com/questions/717690/asp-net-mvc-pass-array-object-as-a-route-value-within-html-actionlink
http://stackoverflow.com/questions/8391055/passing-an-array-to-routevalues-and-have-it-render-model-binder-friendly-url
Basically this would involve special-casing IEnumerable values in a RouteValueDictionary, and generating repeated keys. For example:
routeValues.Add("productId", new[] {1, 3, 5});
would result in a Url being generated something like:
http://server/Controller/Action?productId=1&productId=3&productId=5
As far as I can see with Reflector, the generation of the Url is currently delegated to System.Web.Routing.ParsedRoute.Bind(), which is a Framework class and isn't going to change any time soon.
But this could be achieved in the MVC framework by modifying Url.GenerateUrl as follows:
- Clone the "routeValues" dictionary, and remove all elements whose value is IEnumerable. Pass the cloned dictionary to "routeCollection.GetVirtualPathForArea(requestContext, routeName, values)", which will generate a Url that excludes the IEnumerable routeValues.
- Append the IEnumerable routeValues as queryString parameters.
By making this change in UrlHelper.GenerateUrl, it would automatically become available for all helpers that use UrlHelper.GenerateUrl (UrlHelper.Action, LinkExtensions.ActionLink, etc).