I am retrieving alot of data from the datatabse which bring it into a paged list and datatable. I would like to reverse the order so my new transactions are displayed on the first page and not last page.Everything works good except i need to bring the latest to the first page
Controller Code
using Deadfiles.Data;
using Deadfiles.Models;
using Deadfiles.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.EntityFrameworkCore;
namespace Deadfiles.Controllers
{
public class DeadfilesController : Controller
{
private readonly ApplicationDbContext _context;
public DeadfilesController(ApplicationDbContext context)
{
_context = context;
}
// GET: Deadfiles
[Authorize(Roles = "Admin,User,Viewer")]
public Task<IActionResult> Index(int pg = 1)
{
List<Deadfile> deadfiles = [.. _context.Deadfiles];
const int pageSize = 10;
if (pg < 1)
pg = 1;
var recsCount = deadfiles.Count;
var pager = new Pager(recsCount, pg, pageSize);
int recSkip = (pg - 1) * pageSize;
var data = deadfiles.Skip(recSkip).Take(pageSize).ToArray();
this.ViewBag.Pager = pager;
// return Task.FromResult<IActionResult>(View(data));
if (User.Identity.IsAuthenticated)
{
return Task.FromResult<IActionResult>(View(data));
}
else
{
return Task.FromResult<IActionResult>(this.Redirect("~/identity/account/login"));
}
}
Index Code
@using Microsoft.AspNetCore.Identity @using Microsoft.AspNetCore.Identity
@model IEnumerable<ApplicationUser>
@{
ViewData["Title"] = "Users";
}
<h1>Registered Users</h1>
<p>
<a asp-action="Create" class="btn btn-primary"> <i class="fa fa-plus"></i> Add New User</a>
</p>
<div class="card">
<div class="card-header">
<h3 class="card-title">User List</h3>
</div>
<!-- /.card-header -->
<div class="card-body">
<table id="rolesTable" class="table table-bordered table-striped">
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Email Address</th>
<th>User Role</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelitem => item.FirstName)</td>
<td>@Html.DisplayFor(modelitem => item.LastName)</td>
<td>@Html.DisplayFor(modelitem => item.Email)</td>
<td>@Html.DisplayFor(modelitem => item.Role.Name)</td>
<td><a asp-action="Edit" asp-controller="Account" asp-route-id="@item.Id" class="btn btn-primary"><i class=" fa fa-pencil"></i>Edit</a></td>
<td><a asp-action="Delete" asp-controller="Account" asp-route-id="@item.Id" class="btn btn-danger"><i class=" fa fa-edit"></i>Delete</a></td>
</tr>
}
</tbody>
</table>
</div>
</div>
Paging Model
namespace Deadfiles.Models
{
public class Pager
{
public int TotalItems { get; private set; }
public int CurrentPage { get; private set; }
public int PageSize { get; private set; }
public int TotalPages { get; private set; }
public int StartPage { get; private set; }
public int EndPage { get; private set; }
public Pager()
{
}
public Pager(int totalItems, int page, int pageSize = 30)
{
int totalPages = (int)Math.Ceiling((decimal)totalItems / (decimal)pageSize);
int currentPage = page;
int startPage = currentPage - 5;
int endPage = currentPage + 4;
if (startPage <= 0)
{
endPage = endPage - (startPage - 1);
startPage = 1;
}
if (endPage > totalPages)
{
endPage = totalPages;
if (endPage > 10)
{
startPage = endPage - 9;
}
}
TotalItems = totalItems;
CurrentPage = currentPage;
PageSize = pageSize;
TotalPages = totalPages;
StartPage = startPage;
EndPage = endPage;
}
}
}
I have spent days trying to get this to work