diff options
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 |
