@@ Html.PagedListPager不在查询字符串中发送页面

问题描述 投票:0回答:2

我正在研究ASP.NET Core项目,并将该项目更新为.NET Core 2.2之后,出现了分页问题。我正在使用X.PagedList库。我认为页面数正确,但是问题是,例如,当我尝试访问第二个页面时,该页面未在查询字符串中发送。这是我在视图中的代码:

 @Html.PagedListPager((IPagedList)Model.Products,
           page => Url.Action("ProductsByCategory",
           new { Model.CategoryId, page, Model.SubCategoryId }),
           new PagedListRenderOptions()
           {
             UlElementClasses = new List<string> { "pagination"},
             LiElementClasses = new List<string> { "page-item", "page-link"}
           })

这是我在服务中的代码:

 public AllProductsViewModel GetProductsByCategory(Guid categoryId, int? page, Guid? subCategoryId = null)
    {
        var products = dbContext.Products
            .Where(p => p.CategoryId == categoryId && p.IsAvailable)
            .To<ProductViewModel>()
            .ToList();

        if (subCategoryId != null)
        {
            products = products.Where(p => p.SubCategoryId == subCategoryId).ToList();
        }

        var nextPage = page ?? 1;

        var allProducts = new AllProductsViewModel()
        {
            CategoryId = categoryId,
            SubCategoryId = subCategoryId,
            Products = products.ToPagedList(nextPage, 9)
        };

        return allProducts;
    }
pagination asp.net-core-mvc
2个回答
1
投票

这似乎是在.Net Core 2.2中使用X.PagedList的错误,请检查github上的相关线程:

https://github.com/dncuug/X.PagedList/issues/133

https://github.com/dncuug/X.PagedList/issues/131

因此,请尝试使用pageNumber而不是page作为解决方法。


0
投票

问题归结于asp.net core 2.2构造其URL的方式的变化。默认情况下,它现在检查应用程序中是否有匹配的端点,如果没有,则不输出href。

Routing differences between asp.net core 2.2 and earlier versions

我有已经被使用查询字符串?page的搜索引擎索引的页面,并且我不想包含301来将?page重定向到?pagenumber。

相反,在启动时有一个选项,您可以在其中重写此行为,并允许X.PagedList仍使用页面而不是页面编号来工作。

在添加MVC的位置禁用端点路由功能:

opt.EnableEndpointRouting = false

services.AddMvc(opt => opt.EnableEndpointRouting=false)
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
© www.soinside.com 2019 - 2024. All rights reserved.