how may i add integer list to route

限于喜欢 提交于 2019-12-04 22:00:19

问题


I'm trying to add int[] array to my Url.Action something like this:

var routeData = new RouteValueDictionary();

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

in my view:

Url.Action("Index", routeData)

but in html I'm getting:

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

and not:

GroupsId=1213&GroupsId=1214

I need it because I have a checkbox list, and when I do post I'm binding groupIds to int[] and then pass to view where I have export button with Url.Action that doesn't work correctly for me.


回答1:


You cannot generate such url using the standard helpers because of the duplicate query string parameter names. It's unfortunate but it is how things are.

Here's what you can instead in order to generate such url:

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();


来源:https://stackoverflow.com/questions/6395290/how-may-i-add-integer-list-to-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!