The following microsoft turotial will help:
Tutorial: Add sorting, filtering, and paging - ASP.NET MVC with EF Core
https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/sort-filter-page?view=aspnetcore-6.0
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I work on asp.net core 6 vs 2022 i face issue I can't display Pagination of view action Index of controller Cinemas .
what I try is
1- create custom view model for Cinemas as below :
public class CinmaPageViewModel
{
public IEnumerable<CinimaViewModels> Cinmas { get; set; }
public Pager Pager { get; set; }
}
public class CinimaViewModels
{
public string Logo { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public class Pager
{
public int NumberOfPages { get; set; }
public int CurrentPage { get; set; }
public int TotalRecords { get; set; }
}
2-create service and implementation class inheritance
public interface ICinemasService:IEntityBaseRepository<Cinema>
{
IQueryable<CinimaViewModels>GetAllCinimas();
}
public class CinemasService:EntityBaseRepository<Cinema>, ICinemasService
{
private readonly AppDbContext _context;
public CinemasService(AppDbContext context) : base(context)
{
_context = context;
}
public CinemasService(AppDbContext context) : base(context)
{
_context = context;
}
public IQueryable<CinimaViewModels> GetAllCinimas()
{
var response = _context.Cinemas.Select(x => new CinimaViewModels() { Logo = x.Logo, Name = x.Name,Description=x.Description });
return response;
}
3- on controller I return custom model view CinmaPageViewModel
[AllowAnonymous]
public IActionResult Index(int pageNumber = 1)
{
var allCinemas = _service.GetAllCinimas();
var result = _pageHelper.GetPage(allCinemas.AsQueryable(), pageNumber);
var cinimas = new CinmaPageViewModel
{
Cinmas = result.Items,
Pager = result.Pager
};
return View(cinimas);
}
4-I create view Index as Index.cshtml for action Index as below
<div class="row">
<div class="col-md-8 offset-md-2">
<table class="table">
<thead>
<tr class="text-center">
<th>logo</th>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Cinmas)
{
<tr>
<td class="align-middle">
<img class="rounded-circle" src="@item.Logo" alt="@item.Name" style="max-width: 150px" />
</td>
<td class="align-middle">
@Html.DisplayFor(modelItem => item.Name)
</td>
<td class="align-middle">
@Html.DisplayFor(modelItem => item.Description)
</td>
</tr>}
</tbody>
</table>
</div>
</div>
How to display pagination on index.cshtml view for action Index controller name Cinemas
The following microsoft turotial will help:
Tutorial: Add sorting, filtering, and paging - ASP.NET MVC with EF Core
https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/sort-filter-page?view=aspnetcore-6.0