PagedList 使用 LINQ Skip and Take,但使用结果计数显示分页 [英] PagedList using LINQ Skip and Take, but show paging using Count of results

查看:16
本文介绍了PagedList 使用 LINQ Skip and Take,但使用结果计数显示分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据类别过滤器和 ItemsPerPage 显示过滤后的产品列表,但在尝试将其与 PagedList 一起使用时遇到了一些问题.

如果我需要编写自己的分页代码,或者有没有办法使用 PagedList 获得我需要的结果,那么具有 PagedList 专业知识的人可以建议我.

我正在使用 LINQ 的 Skip &采取函数只获取当前页面需要显示的行数,但我仍然希望分页链接根据过滤器的总数显示页面.

例如:我的搜索过滤器找到 50 个结果,但由于我每页的行是 10 个项目,我使用 LINQ 的 Skip() &Take() 只返回 10 行.我仍然需要在我的 View.cshtml 中显示页面链接 <<1 |2 |3 |4 |5 >>现在使用默认的 PagedList,我只得到 <<1 >>,我知道为什么我只看到一个页面,但只是想知道如何让它显示正确数量的页面链接,同时只获得结果的一个子集.

**我的目标是将优化的查询写入数据库,以便网页响应性能更快.

这是我的 Action Method 代码的样子.代码得到了正确的结果,但分页无法正常工作:

public ViewResult List(int page =1, string category =null){if (category != null) this.CurrentCategory = category;var products = repository.Products.Where(p => this.CurrentCategory == null || p.Category == this.CurrentCategory).OrderBy(p => p.ProductID).Skip((page -1) * PageSize).Take(PageSize);返回视图(products.ToList().ToPagedList(page, PageSize));}

这是处理分页的视图中的代码片段.我查看了该项目的 Github 站点,但找不到提供自定义页面的扩展方法.我认为它只会根据 @Model 中的每页项目数"和产品的 Count() 呈现页数:

@model IPagedList//渲染@Model的foreach循环//使用PagedList显示分页的代码<div style="text-align:center">@Html.PagedListPager(Model, page => Url.Action("List", new { page = page, category = ViewBag.CurrentCategory }), PagedListRenderOptions.OnlyShowFivePagesAtATime)

解决方案

我遇到了完全相同的问题,最终我使用了 StaticPagedList.你可以这样做

public ViewResult List(int page =1, string category =null){if (category != null) this.CurrentCategory = category;var products = repository.Products.Where(p => this.CurrentCategory == null || p.Category == this.CurrentCategory).OrderBy(p => p.ProductID).Skip((page -1) * PageSize).Take(PageSize);var count = repository.Products.Where(p => this.CurrentCategory == null || p.Category == this.CurrentCategory).Count();var resultAsPagedList = new StaticPagedList(products, page, PageSize, count);返回视图(resultAsPagedList);}

至于视图,你只需要替换模型类型

@model StaticPagedList

I am trying to display a filtered list of of products, based on Category filter and ItemsPerPage but I'm having some issues when trying to use it with PagedList.

Someone with PagedList expertise could advice me if I need to write my own pagination code or is there a way to get the results I need using PagedList.

I am using LINQ's Skip & Take functions to get only the number of rows that need to be displayed on the current page, but I would still like paging links to show pages based on the filter's total count.

E.g.: my search filter finds 50 results, but since my rows per page is say 10 items, I use LINQ's Skip() & Take() to get only 10 rows back. I still need to show in my View.cshtml the page links << 1 | 2 | 3 | 4 | 5 >> Right now with default PagedList, I only get << 1 >>, I know why I only see one page but just wanted to know how can I make it work to show correct number of page links, while only getting a subset of results.

**My goal is to write optimized queries to the Database so the web page response performance will be fast.

Here is what my code for the Action Method looks like. The code is getting the correct results but the pagination is not working as I need it to be:

public ViewResult List(int page =1, string category =null)
{
    if (category != null) this.CurrentCategory = category;

    var products = repository.Products
                    .Where(p => this.CurrentCategory == null || p.Category == this.CurrentCategory)
                    .OrderBy(p => p.ProductID)
                    .Skip((page -1) * PageSize)
                    .Take(PageSize);

    return View(products.ToList().ToPagedList(page, PageSize));
}

Here is the code snippet from the View that deals with pagination. I looked into the project's Github site but could not find an extension method to provide custom pages. I think it will only render number of pages based on 'items per page' and the Count() of products in the @Model:

@model IPagedList<Product>

//foreach loop that renders the @Model

//Code that displays the pagination using PagedList
<div style="text-align:center">
    @Html.PagedListPager(Model, page => Url.Action("List", new { page = page, category =  ViewBag.CurrentCategory }), PagedListRenderOptions.OnlyShowFivePagesAtATime
    )
</div>

解决方案

I had the exactly same problem and I ended up using StaticPagedList. You can do something like this

public ViewResult List(int page =1, string category =null)
{
    if (category != null) this.CurrentCategory = category;

    var products = repository.Products
                   .Where(p => this.CurrentCategory == null || p.Category == this.CurrentCategory)
                   .OrderBy(p => p.ProductID)
                   .Skip((page -1) * PageSize)
                   .Take(PageSize);

var count = repository.Products
                   .Where(p => this.CurrentCategory == null || p.Category == this.CurrentCategory).Count();

var resultAsPagedList = new StaticPagedList<Product>(products, page, PageSize, count);

    return View(resultAsPagedList);
}

as for the view, you just need to replace the model type

@model StaticPagedList<Product>

这篇关于PagedList 使用 LINQ Skip and Take,但使用结果计数显示分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆