Pagination not working properly in ASP.NET Core 2.1 app

Farid GN 1 Reputation point
2024-07-27T15:06:16.6233333+00:00

Hi guys,

I had implemented pagination using IPagedList with model which was successful for my ads classifieds website. This time I needed to apply with ViewModel as several models needed in this view and used StaticPagedList as it wasn't working with IPagedList. I'm not facing any error but all pages that was distributed according to post counts displays all the posts (like in 1 - all 64, in 2-again same 64).

Here is Action in Conrtoller:

public async Task<IActionResult> Category(int Id, int page = 1, int pageSize = 32)
        {
            category_id = Id;
            var postdata = _offerDbContext.Posts.Where(y => y.Subcategory.CategoryId == Id).OrderByDescending(y => y.Id);
            SubViewModel subModel = new SubViewModel();            
            subModel.Subcategories = await _offerDbContext.Subcategories.Where(x => x.CategoryId == Id).ToListAsync();
            
            subModel.Companies = await _offerDbContext.Companies.ToListAsync();
           
            subModel.Category = await _offerDbContext.Categories.SingleOrDefaultAsync(c => c.Id == Id);
            var postcount = _offerDbContext.Posts.Where(z => z.Subcategory.CategoryId == Id).Count();
           
            subModel.Pagedlist = new StaticPagedList<Post>(postdata, page, pageSize, postcount);
            await _offerDbContext.SaveChangesAsync();
            
            return View(subModel);
        }
//Note that I have no compile time error, all needed namespaces were provided.
And Here is the View:(part of view which concerns this case)
@model  Foroffer.Models.ViewModels.SubViewModel
@using PagedList.Core.Mvc
@foreach (Post item in Model.Pagedlist)
            { 
                //body part with item features
           }
<table border="0" class="pagetable">
            <tr>
                <td colspan="5" align="center" class="listedpage">
                   <pager class ="pager" list="@Model.Pagedlist" asp-action="Category" asp-controller="Home" asp-route-model ="@Model" param-page-number="page" />
                     
                </td>
            </tr>
        </table>	

I also tired pager-list help tag which also gave the same result. Note that it allocates number of pages correctly according to total number of posts, but it stores all the posts in each page which makes it useless. I'm suspicious about helper tag work, and not that @Html.PagedlistPager not working, it cannot find this tag though I installed both IPagedlist.Core and IPagedlist.MVC. Maybe the problem is in action, I can't find. Please, help me fix this.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,405 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,419 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,655 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 61,731 Reputation points
    2024-07-27T16:53:21.9833333+00:00

    Asp.net core 2.1 has been out of support for years. Also PagedList is no longer supported, but there is a fork:

    https://github.com/dncuug/X.PagedList

    0 comments No comments

  2. SurferOnWww 2,491 Reputation points
    2024-07-28T00:57:37.21+00:00

    As I do not understand your issue, I am sorry if my answer below does not help.

    but all pages that was distributed according to post counts displays all the posts (like in 1 - all 64, in 2-again same 64).

    I suggest that you consider using the PaginatedList<T> class described in the following Microsoft tutorial:

    Add paging to Students Index

    I also tired pager-list help tag which also gave the same result. Note that it allocates number of pages correctly according to total number of posts, but it stores all the posts in each page which makes it useless.

    Although the pager shown in the above tutorial includes only [Previous] and [Next] links, you will able to modify it using Bootstrap Pagination. Shown below is a sample:

    enter image description here

    0 comments No comments