【问题标题】:how may i add integer list to route我如何将整数列表添加到路由
【发布时间】:2011-09-17 17:26:27
【问题描述】:

我正在尝试将 int[] 数组添加到我的 Url.Action 中,如下所示:

var routeData = new RouteValueDictionary();

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

在我看来:

Url.Action("Index", routeData)

但在 html 中我得到:

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

而不是:

GroupsId=1213&GroupsId=1214

我需要它,因为我有一个复选框列表,当我发布时,我将 groupIds 绑定到 int[],然后通过对我来说无法正常工作的 Url.Action 来查看我有导出按钮的位置。

【问题讨论】:

    标签: asp.net-mvc-3 binding asp.net-mvc-routing url.action


    【解决方案1】:

    由于查询字符串参数名称重复,您无法使用标准帮助程序生成此类 url。这很不幸,但事情就是这样。

    为了生成这样的 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();
    

    【讨论】:

      猜你喜欢
      • 2021-09-29
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      • 1970-01-01
      • 2019-01-21
      • 2021-10-07
      • 2021-03-03
      • 1970-01-01
      相关资源
      最近更新 更多