How can I add an integer list to a route

I am trying to add an int [] array to my Url.Action like this:

var routeData = new RouteValueDictionary(); for (int i = 0; i < Model.GroupsId.Length; i++) { routeData.Add("GroupsId[" + i + "]", Model.GroupsId[i]); } 

in my opinion:

 Url.Action("Index", routeData) 

but in html i get:

 GroupsId%5B0%5D=1213&GroupsId%5B0%5D=1214 

and not:

 GroupsId=1213&GroupsId=1214 

I need this because I have a list of checkboxes, and when I post a message, I bind groupIds to int [], and then go on to view where I have an export button with Url.Action, which doesn't work correctly for me.

+7
source share
1 answer

You cannot create such a URL using standard helpers because of duplicate query string parameter names. This is sad, but the way it is.

Here you can create the following URL instead:

 var baseUrl = Url.Action("Index", "SomeController", null, Request.Url.Scheme); var uriBuilder = new UriBuilder(baseUrl); uriBuilder.Query = string.Join("&", Model.GroupsId.Select(x => "groupsid=" + x)); string url = uriBuilder.ToString(); 
+6
source

All Articles