对数组使用asp-route- {variable}标签帮助程序

时间:2019-01-22 21:17:46

标签: c# asp.net-core razor-pages

我正在向this ASP.NET Core Razor Pages tutorial学习,我正在努力使其适应我的需求。对于分页链接,缩短为:

<a asp-page="./Index"
   asp-route-pageIndex="@(Model.Student.PageIndex + 1)"
   asp-route-currentFilter="@Model.CurrentFilter"
   class="btn btn-default">
    Next
</a>

我找不到指示如何处理asp-route- {variable}的说明,其中变量(代码段中的currentFilter)是一个数组。就我而言,在我看来,我已经将CurrentFilter调整为具有multiple属性的选择框,它在URL中显示如下:

https://localhost/Student/?currentFilter=foo&currentFilter=bar

以字符串数组的形式进入我的模型。我找不到有关如何使用asp-route标签帮助程序将数组传递到查询字符串中的任何文档或解决方案。

2 个答案:

答案 0 :(得分:0)

我一直在尝试变态的解决方法,直到得到一个很好的答案...

我已经更新了CSHTML以使用asp-all-route-data。

@{
    var nextParms = new Dictionary<string, string>();

    int x = 0;
    nextParms.Add("pageIndex", (Model.Mini.PageIndex + 1).ToString());
    foreach (string item in Model.CurrentFilter)
    {
        nextParms.Add("SearchString" + x, item);
        x++;
    }
}

<a asp-page="./Index"
   asp-all-route-data="nextParms"
   class="btn btn-default">
    Next
</a>

然后,如果OnGet方法中没有CurrentFilterN并且没有CurrentFilter,则重新构建CurrentFilter。

        if (CurrentFilter !=null && CurrentFilter .Count()>0)
        {
            //Logic if CurrentFilter exists as normal
        }
        else
        {
           List<string> SearchList = new List<string>();

            foreach (var key in HttpContext.Request.Query)
            {
                if (key.Key.Contains("SearchString"))
                {
                    SearchList.Add(key.Value);
                    string IndividualTag = key.Value;
                }
                //Same logic as above
            }

            CurrentFilter = SearchList.ToArray();
        }

因此,如果用户使用多选,则会正确设置CurrentFilter。如果单击下一步,则将SearchString0,SearchString1,...,SearchStringN传递到查询字符串中,该查询字符串将解析为CurrentFilter。

感觉很hack,但是可以。

答案 1 :(得分:0)

我最终将值放入逗号分隔的字符串中,然后使用string.Split(',').Select(s=>int.Parse(s))