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

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

问题描述

我想显示产品的过滤列表,基于类别过滤器和ItemsPerPage而是试图与PagedList使用它时,我有一些问题。

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.

有人用PagedList的专业知识,如果我需要写我自己的分页code还是有办法让我需要使用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.

我使用LINQ的跳过&安培;以函数来获取只需要在当前页面上显示的行数,但我还是想分页链接基于过滤器的总数,显示的页面。

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.

例如:我的搜索过滤器找到50个结果,但由于每页行我说的是10个项目中,我使用LINQ的跳过()及以()只得到10行回来。我还需要在我的View.cshtml显示页面链接的<< 1 | 2 | 3 | 4 | 5 >>
眼下默认PagedList,我只得到的<< 1 >> ,我知道为什么我只看到一个页面,但只是想知道我怎么可以让它工作来展示页面链接的正确数量,而只得到结果的一个子集。

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.

下面是我的code的操作方法是什么样子。在code为得到正确的结果,但分页不工作,因为我需要它是:

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));
    }

下面是与分页交易的查看code片段。我看着这个项目的Github上的网站,但无法找到一个扩展方法以提供自定义的页面。我认为它只会呈现在@Model产品的基础上每页的项目页)号和计数(

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
)

推荐答案

我有完全相同的问题,我结束了使用StaticPagedList。你可以做这样的事情。

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跳过和Take,但使用效果显示计数分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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