PagedList using LINQ Skip and Take, but show paging using Count of results

前端 未结 5 1282
眼角桃花
眼角桃花 2020-12-15 06:02

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.

So

5条回答
  •  天涯浪人
    2020-12-15 06:19

    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(products, page, PageSize, count);
    
        return View(resultAsPagedList);
    }
    

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

    @model StaticPagedList
    

提交回复
热议问题