ASP.NET Core Razor Pages多选输入和分页

时间:2019-01-18 21:44:23

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

我正在关注ASP.NET Core Razor Pages的官方Microsoft教程,并且我正在调整位于here的分页部分以适合我的网站。

public async Task OnGetAsync(string sortOrder,
    string currentFilter, string searchString, int? pageIndex)
{
    CurrentSort = sortOrder;
    NameSort = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
    DateSort = sortOrder == "Date" ? "date_desc" : "Date";
    if (searchString != null)
    {
        pageIndex = 1;
    }
    else
    {
        searchString = currentFilter;
    }

    CurrentFilter = searchString;

    IQueryable<Student> studentIQ = from s in _context.Student
                                    select s;
    if (!String.IsNullOrEmpty(searchString))
    {
        studentIQ = studentIQ.Where(s => s.LastName.Contains(searchString)
                               || s.FirstMidName.Contains(searchString));
    }
    switch (sortOrder)
    {
        case "name_desc":
            studentIQ = studentIQ.OrderByDescending(s => s.LastName);
            break;
        case "Date":
            studentIQ = studentIQ.OrderBy(s => s.EnrollmentDate);
            break;
        case "date_desc":
            studentIQ = studentIQ.OrderByDescending(s => s.EnrollmentDate);
            break;
        default:
            studentIQ = studentIQ.OrderBy(s => s.LastName);
            break;
    }

    int pageSize = 3;
    Student = await PaginatedList<Student>.CreateAsync(
        studentIQ.AsNoTracking(), pageIndex ?? 1, pageSize);
}

对于我的网站,等效于SearchString是一个多选选择框。这将作为字符串数组传递到页面。我找不到任何有关如何将其格式化为适用于普通字符串的链接的cshtml部分的文档。

基本上,我需要获取下面的asp-route-currentFilter才能处理字符串数组。

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

我该怎么做?

1 个答案:

答案 0 :(得分:0)

asp-route-CurrentFilter无法分配数组。您可以使用@Url.Page分配自定义路线数据。请参考here

public string[] CurrentFilter { get; set; }
public async Task OnGetAsync(string sortOrder,string[] currentFilter,string[] searchString,int? pageIndex)

.cshtml:

<a href="@Url.Page("./Index", 
                    new {
                         sortOrder= Model.CurrentSort,
                         currentFilter = Model.CurrentFilter, 
                         pageIndex= Model.Student.PageIndex +1
                    })"
                    class="btn btn-default @nextDisabled">
Next</a>