PagedList and Async
Asked Answered
B

2

13

I'm using PagedList in my Views, but my scaffolded Controller is generated with this kind of default Index Action:

public async Task<ActionResult> Index()
{
    return View(await db.Claimants.ToListAsync());
}

I didn't find an extension for PagedList to work with async. My methods have to be changed to a form like this:

public ActionResult Index(int? page)
{
    var claimants = db.Claimants.OrderBy(b => b.Name);
    var notNullPage = page ?? 1;
    return View(claimants.ToPagedList(notNullPage, 50));
}

Is there a reasonable way to work with PagedList and async?

Bookcraft answered 22/7, 2015 at 21:29 Comment(0)
K
20

Is there a reasonable way to work with PagedList and async?

Yes. Doesn't come out of the box with EF. But, you can get PagedList.EntityFramework which exposes a ToPagedListAsync:

public class PagedListExtended<T> : BasePagedList<T> 
{
    private PagedListExtended() 
    {
    }

    public static async Task<IPagedList<T>> Create(IQueryable<T> superset, int pageNumber, int pageSize) 
    {
        var list = new PagedListExtended<T>();
        await list.Init(superset, pageNumber, pageSize);
        return list;
    }

    async Task Init(IQueryable<T> superset, int pageNumber, int pageSize)
    {
        if (pageNumber < 1)
              throw new ArgumentOutOfRangeException("pageNumber", pageNumber, "PageNumber cannot be below 1.");
        if (pageSize < 1)
             throw new ArgumentOutOfRangeException("pageSize", pageSize, "PageSize cannot be less than 1.");
        TotalItemCount = superset == null ? 0 : await superset.CountAsync();
        PageSize = pageSize;
        PageNumber = pageNumber;
        PageCount = TotalItemCount > 0 ? (int) Math.Ceiling(TotalItemCount/(double) PageSize) : 0;
        HasPreviousPage = PageNumber > 1;
        HasNextPage = PageNumber < PageCount;
        IsFirstPage = PageNumber == 1;
        IsLastPage = PageNumber >= PageCount;
        FirstItemOnPage = (PageNumber - 1)*PageSize + 1;
        var num = FirstItemOnPage + PageSize - 1;
        LastItemOnPage = num > TotalItemCount ? TotalItemCount : num;
        if (superset == null || TotalItemCount <= 0)
            return;
        Subset.AddRange(pageNumber == 1 ? await superset.Skip(0).Take(pageSize).ToListAsync() : await superset.Skip((pageNumber - 1)*pageSize).Take(pageSize).ToListAsync());
    }
}

public static class PagedListExtendedExtensions 
{
     public static async Task<IPagedList<T>> ToPagedListAsync<T>(this IQueryable<T> superset, int pageNumber, int pageSize)
     {
         return await PagedListExtended<T>.Create(superset, pageNumber, pageSize);
      }
}
Katharinakatharine answered 22/7, 2015 at 21:57 Comment(4)
Worked like a charm. Thank you!Bookcraft
Github link is brokenWadsworth
Working with ASP.NET MVC5, I recommend this over upgrading to the X.PagedList version. X.PagedList downloads a bunch of unnecessary .Net Core dependencies and added warnings to my views that I couldn't get rid of.Sokotra
It looks like that project has gone stale. No updates for the last 4 years.Crouse
M
12

Yes, you can.

You can use X.PagedList which is an improved version of PagedList and supports async operations.

May answered 13/5, 2016 at 16:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.