I can't add pagination of action Index of controller cinimas on asp.net core 6 ?

ahmed salah 3,216 Reputation points
2022-12-07T01:19:20.92+00:00

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

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. SurferOnWww 4,951 Reputation points
    2022-12-07T02:14:29.797+00:00

    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


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.