Xenosphere
Xenosphere

Reputation: 15

How to Properly Implement Pagination in ASP.NET CORE MVC

What is the most preferred and efficient way to implement pagination into an ASP.NET Core MVC project?

I currently have a .cshtml view that will allow the user to view their help desk tickets that they have submitted. In terms of the front end, the table that I am using is of class table-responsive table-responsive-data2. I want to show as many entries as the user decides.

Hard coded example

What is the best way to implement this while working with ASP.NET Core MVC?

EDIT: The name column would be taken away, since its the same user making the tickets

Below is the entire ticket entity (Tickekt.cs):

namespace HelpDeskTicket.Models
{
    public class Ticket
    {
        public string EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public int Id { get; set; }
        public string Department { get; set; }
        public string Title { get; set; }
        [StringLength(225)]
        public string TicketDescription { get; set; }
        public byte[] Attachment { get; set; }
        public string AttachmentName { get; set; }
        public string AttachmentExtension { get; set; }
        public DateTime CreatedDate { get; set; }

        [Required]
        public TicketStatus Status { get; set; } 
    }

    public enum TicketStatus
    {
        Resolved,
        InProgress,
        Unresolved
    }
}

  //How would I modify the TicketController.cs in order to implement Pagination properly
 public IActionResult MyTickets(Employee employee) => View();

Upvotes: 1

Views: 9324

Answers (1)

Fei Han
Fei Han

Reputation: 27793

What is the most preferred and efficient way to implement pagination into an ASP.NET Core MVC project?

If you'd like to implement server side pagination functionality, you can add paging buttons (such as "Previous", "Next" etc) in your view page, and pass pageIndex and pageSize etc to action method while user click paging button.

In your action method, you can filter and return paged data using Skip and Take statements based on the pageIndex and pageSize that user passed.

var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();

This tutorial explained with example how to implement paging functionality, you can refer to it.

https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/sort-filter-page?view=aspnetcore-3.1#add-paging-to-students-index

Upvotes: 4

Related Questions