Home > Net >  Pagination for the view which is not using the EF
Pagination for the view which is not using the EF

Time:11-02

I am trying to add pagination to the Index()/ the view , this index view uses the custom Model this is not a table from the DB. I am trying to see if I can use the PaginatedList.cs, as my other pages in the same project uses the EF and have pagination working great.

Below is is my index()

  public async Task<IActionResult> Index(string custEmail,int pageNumber = 1)
    {
        List<Order> oo = new List<Order>();

        var timeUtc = DateTime.UtcNow;
        var easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
        var todayDt = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, easternZone);

        var orderData = from mo in _context.MouseOrders
                        join io in _context.InventoryOrders on mo.MouseOrderId equals io.OrderId
                        join cus in _context.Customers on mo.CustomerId equals cus.CustomerId
                        join i in _context.Inventories on io.InventoryId  equals i.InventoryId
                        where mo.SamaccountEmail == custEmail 
                        && mo.OrderDate.Date == todayDt.Date
                        select new
                        {
                           CustomerNumber= cus.CustomerNumber,
                           OrderID = mo.MouseOrderId,
                           Genotype = i.Genotype,
                            RoomNumber = i.RoomNumber,
                            QuantityAvailable = i.QuantityAvailable,
                            OrderQuantity = io.OrderQuantity
                        };

       foreach (var item in orderData)
        {
            Order o = new Order();
            o.CustomerNumber = item.CustomerNumber;
            o.OrderId = item.OrderID;
            o.Genotype = item.Genotype;
            o.RoomNumber = item.RoomNumber;
            o.QuantityAvailable = item.QuantityAvailable;
            o.OrderQuantity = item.OrderQuantity;
            oo.Add(o);
        }
        return View(await PaginatedList<Order>.CreateAsync(oo,pageNumber,5));
    }

But I get error like

Cannot convert from 'System.Collections.Generic.List<JSMApp.Models.Order>' to 'System.Linq.IQueryable<JSMApp.Models.Order>'

I tried changing the List List<Order> to IQueryable but even that is throwing error.

I am trying to have the same pagination behavior/look and feel that I have for the other pages that uses the EF and PaginatedList but not sure how to do for the Models that are custom and not from DB

EDIT* When I am trying to access the Index() page I am getting the below error

An unhandled exception occurred while processing the request. NotImplementedException: The method or operation is not implemented. JSMApp.PaginatedList<T>.CreateAsync<TEntity>(IQueryable<TEntity> entities, int v, int pageSize) in PaginatedList.cs, line 53

Below is the line where it is throowing the error in PaginatedList

 internal static Task<string> CreateAsync<TEntity>(IQueryable<TEntity> entities, int v, int pageSize) where TEntity : class
    {
        throw new NotImplementedException();
    }

CodePudding user response:

You're almost there. Just change;

select new
{
    CustomerNumber= cus.CustomerNumber,
    OrderID = mo.MouseOrderId,
    // etc
}

to

select new Order
{
    CustomerNumber= cus.CustomerNumber,
    OrderID = mo.MouseOrderId,
    // etc
}

Now your orderData will be an IQueryable<Order> that can be passed;

PaginatedList<Order>.CreateAsync(orderData,pageNumber,5))
  • Related