1

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();
1
  • 1
    I like Fei Han's answer below. Just as a general tip. When you're dealing with a situation like this, start at the data access layer and figure out what you would need to do there. Then work your way up to the business layer and then the controller. E.g. in the db stuff, you'd need to know how many pages to skip and how many records to return. So this gives you a clue that you'd need to pass those two parameters back from the view to the controller all the way down to the DB. Commented Nov 5, 2020 at 7:09

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.