diff options
| author | Paweł Bernaciak <pawelbernaciak@zohomail.eu> | 2023-12-22 16:02:41 +0100 |
|---|---|---|
| committer | Paweł Bernaciak <pawelbernaciak@zohomail.eu> | 2023-12-22 16:02:41 +0100 |
| commit | 1a96616699ab41bf6343bc1acc45a836c3e6caf3 (patch) | |
| tree | aff3d8bb8418355d3a8279024afaa13f196a0506 /CoreWiki/Pages/All.cshtml.cs | |
| parent | 7fefe217c8462444ecb9806599e70afe2a7102ea (diff) | |
Backup
Diffstat (limited to 'CoreWiki/Pages/All.cshtml.cs')
| -rw-r--r-- | CoreWiki/Pages/All.cshtml.cs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/CoreWiki/Pages/All.cshtml.cs b/CoreWiki/Pages/All.cshtml.cs new file mode 100644 index 0000000..c41e216 --- /dev/null +++ b/CoreWiki/Pages/All.cshtml.cs @@ -0,0 +1,39 @@ +using CoreWiki.Models; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Internal; + +namespace CoreWiki.Pages; + +public class All : PageModel +{ + private readonly ApplicationDbContext _context; + + [BindProperty(SupportsGet = true)] + public int PageNumber { get; set; } = 1; + [BindProperty(SupportsGet = true)] + public int PageSize { get; set; } = 25; + public IEnumerable<Article>? Articles { get; set; } + public int TotalPages { get; set; } + + public All(ApplicationDbContext context) + { + _context = context; + } + + public async Task<IActionResult> OnGetAsync() + { + TotalPages = (int)Math.Ceiling(await _context.Articles.CountAsync() / (float)PageSize); + if (PageNumber > TotalPages) PageNumber = 1; + + Articles = await _context.Articles + .AsNoTracking() + .OrderBy(a => a.PublishedDateTime) + .Skip(PageSize * (PageNumber - 1)) + .Take(PageSize) + .ToArrayAsync(); + + return Page(); + } +}
\ No newline at end of file |
