Asp.net core 2.1 has been out of support for years. Also PagedList is no longer supported, but there is a fork:
Pagination not working properly in ASP.NET Core 2.1 app
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.
2 answers
Sort by: Most helpful
-
-
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:
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: