question

AntonBrckel-9328 avatar image
0 Votes"
AntonBrckel-9328 asked RuikaiFeng-MSFT commented

.net 6 Core MVC Paging for assigned data

I am trying to build an application like in the tutorial in Microsoft: https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/sort-filter-page?view=aspnetcore-6.0

So i am using the PaginatedList.cs as helper:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Threading.Tasks;
 using Microsoft.EntityFrameworkCore;
    
 namespace ContosoUniversity
 {
     public class PaginatedList<T> : List<T>
     {
         public int PageIndex { get; private set; }
         public int TotalPages { get; private set; }
    
         public PaginatedList(List<T> items, int count, int pageIndex, int pageSize)
         {
             PageIndex = pageIndex;
             TotalPages = (int)Math.Ceiling(count / (double)pageSize);
    
             this.AddRange(items);
         }
    
         public bool HasPreviousPage => PageIndex > 1;
    
         public bool HasNextPage => PageIndex < TotalPages;
    
         public static async Task<PaginatedList<T>> CreateAsync(IQueryable<T> source, int pageIndex, int pageSize)
         {
             var count = await source.CountAsync();
             var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
             return new PaginatedList<T>(items, count, pageIndex, pageSize);
         }
     }
 }

and it runs fine for the Index.

In the Detail i would like to have the fields and a pagnated list from the assigned data:

 public async Task<IActionResult> Details(int? id, int? seiteBL)
         {
             if (id == null || _context.Länder == null)
             {
                 return NotFound();
             }            
    
             var land = await _context.Länder
                 .AsNoTracking()
                 .Include(a => a.Bundesländer.Where(s => s.Aktiv.Equals(true)))
                 .FirstOrDefaultAsync(m => m.Id == id);
                            
             if (land == null)
             {
                 return NotFound();
             }
             var pageSizeBL = Configuration.GetValue("PageSize", 16);
             double AnzahlTrefferBL = land.Bundesländer.Count();
             int? aktuelleSeiteBL = seiteBL;
             if (aktuelleSeiteBL == null)
             {
                 aktuelleSeiteBL = 1;
             }
             ViewData["AnzahlTrefferBL"] = land.Bundesländer.Count();
             ViewData["AnzahlSeitenBL"] = (int)Math.Ceiling(AnzahlTrefferBL / pageSizeBL);
             ViewData["AktuelleSeiteBL"] = aktuelleSeiteBL;
    
             //how do i get the paging of land.Bundesländer by using the PaginatedList?
    
             return View(land);
         }


how do i get the paging of land.Bundesländer by using the PaginatedList?

dotnet-csharpdotnet-aspnet-core-mvc
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered AntonBrckel-9328 commented

as they are separate requests, the the detail request would need to rebuild the list. you would need to add the required parameters to the detail request (page number, page size).

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

My problem is how to do it with in the model assigned data:

 var land = await _context.Länder
                  .AsNoTracking()
                  .Include(a => a.Bundesländer.Where(s => s.Aktiv.Equals(true)))
                  .FirstOrDefaultAsync(m => m.Id == id);

now i need a PaginatedList of land.Bundesländer -- but how to do it?

0 Votes 0 ·
RuikaiFeng-MSFT avatar image
0 Votes"
RuikaiFeng-MSFT answered RuikaiFeng-MSFT commented

Hi,@AntonBrckel-9328
if you checked the codes:


  public static async Task<PaginatedList<T>> CreateAsync(IQueryable<T> source, int pageIndex, int pageSize)
              {
                  var count = await source.CountAsync();
                  var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
                  return new PaginatedList<T>(items, count, pageIndex, pageSize);
              }

you'll find you have to pass the value of pageIndex and pagesize to this method for paging;
If you want to assign the value of pageIndex and pagesize in your Index page and pass them to your controller.
you could add them in the query with asp-route-pageIndex="" and asp-route-page-size=""
the uri looks as below now:
241354-qa9151.png
and you could reveive the arguements in DetailsController as below:

 public async Task<IActionResult> Details(int? id,int PageIndex,int Pagesize //other arguements)
 {
 .........
 }

241339-qa-9152.png

You could check this document for more details of Modelbinding


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Best regards,
RuikaiFeng



qa9151.png (3.3 KiB)
qa-9152.png (21.4 KiB)
· 10
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

for the Index with land it runs perfect;

 return _context.Länder != null ? 
                           View(await PaginatedList<Land>.CreateAsync(länder.AsNoTracking(), seite ?? 1, pageSize)) :
                           Problem("Entity set 'ApplicationDbContext.Länder'  is null.");

241492-2022-09-15.png

but how to do it for the detail-page with the in the model assigned data

 .Include(a => a.Bundesländer.Where(s => s.Aktiv.Equals(true)))

241472-2022-09-15-1.png

can you give me an example for my controller?


0 Votes 0 ·
2022-09-15-1.png (102.0 KiB)
2022-09-15.png (95.6 KiB)

Is the detail-page a partialview of your index page or a new page?

0 Votes 0 ·

it is a new page in the same controller...the part of the controller for the detail-page i posted: (public async Task<IActionResult> Details(int? id, int? seiteBL))

0 Votes 0 ·
Show more comments