Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular
    • Angular js
    • Asp.net Core
    • C
    • C#
    • DotNet
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • Security
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
    • SEO Analyzer
    • Background Remover
  1. Home
  2. Blog
  3. ASP.NET MVC
  4. Simple Pagination in Asp.Net MVC

Simple Pagination in Asp.Net MVC

Date- Feb 12,2023

Updated Mar 2026

7804

Free Download Pay & Download
Simple Pagination Pagination in mvc

Overview of Pagination

Pagination is a technique used to divide a large dataset into smaller, more manageable sections. This is particularly useful in web applications where displaying all data at once can lead to overwhelming user experiences, slow loading times, and unnecessary data clutter. In ASP.NET MVC, pagination can be implemented either on the client side or the server side, each with its own set of advantages.

Client-side pagination involves loading all data at once and then using JavaScript to segment that data into pages. This is suitable for smaller datasets where performance is not a concern. On the other hand, server-side pagination retrieves only a subset of data from the server as needed, making it ideal for larger datasets where performance and speed are paramount.

In this tutorial, we will focus on implementing simple pagination using jQuery for client-side scenarios, as well as discussing how to extend this to server-side pagination for more complex applications.

Simple Pagination

Prerequisites

Before we dive into the implementation, ensure you have the following prerequisites:

  • Basic understanding of ASP.NET MVC framework.
  • Familiarity with HTML, CSS, and JavaScript.
  • jQuery library included in your project.

Setting Up Your Project

To implement pagination in your ASP.NET MVC project, you need to include the necessary libraries and create a sample dataset. Begin by adding the following scripts and CSS to your webpage:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js" integrity="sha512-STof4xm1wgkfm7heWqFJVn58Hm3EtS31XFaagaa8VMReCXAkQnJZ+jEy8PCC/iT18dFy95WcExNHFTqLyp72eQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/simplePagination.js/1.4/simplePagination.css" integrity="sha512-emkhkASXU1wKqnSDVZiYpSKjYEPP8RRG2lgIxDFVI4f/twjijBnDItdaRh7j+VRKFs4YzrAcV17JeFqX+3NVig==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/simplePagination.js/1.4/jquery.simplePagination.js" integrity="sha512-D8ZYpkcpCShIdi/rxpVjyKIo4+cos46+lUaPOn2RXe8Wl5geuxwmFoP+0Aj6wiZghAphh4LNxnPDiW4B802rjQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

Next, create a table with some demo data that we will use to implement pagination:

<div class="row">
    <table id="tableDemo" class="table table-striped">
        <thead>
            <tr>
                <th>Id</th>
                <th>Name</th>
                <th>Address</th>
            </tr>
        </thead>
        <tbody>
            <tr><td>1</td><td>Name 1</td><td>Address 1</td></tr>
            <tr><td>2</td><td>Name 2</td><td>Address 2</td></tr>
            <tr><td>3</td><td>Name 3</td><td>Address 3</td></tr>
            <tr><td>4</td><td>Name 4</td><td>Address 4</td></tr>
            <tr><td>5</td><td>Name 5</td><td>Address 5</td></tr>
        </tbody>
    </table>
</div>

Implementing Client-Side Pagination

Once your setup is complete, we can proceed to implement client-side pagination using the Simple Pagination jQuery plugin. This plugin allows for easy pagination of HTML elements, making it perfect for our table.

To initialize the pagination, we will add a JavaScript function that selects our table and applies the pagination logic. Below is an example of how to do this:

$(document).ready(function() {
    var itemsPerPage = 2;
    var numItems = $('#tableDemo tbody tr').length;
    var pagination = $('#pagination');

    $('#tableDemo').simplePagination({
        items: numItems,
        itemsOnPage: itemsPerPage,
        cssStyle: 'light-theme',
        onPageClick: function(pageNumber) {
            var start = itemsPerPage * (pageNumber - 1);
            var end = start + itemsPerPage;
            $('#tableDemo tbody tr').hide().slice(start, end).show();
        }
    });
});

This code initializes the pagination, specifying the number of items per page and setting up a click event handler to show the correct rows based on the selected page.

Simple Pagination in AspNet MVC

Implementing Server-Side Pagination

Server-side pagination is crucial when dealing with large datasets. Instead of loading all the data at once, we only request the data needed for the current page from the server. This reduces load times and improves performance.

To implement server-side pagination in ASP.NET MVC, you will typically modify your controller to accept parameters for page number and page size, and then query your database accordingly. Here’s an example of a controller action that implements server-side pagination:

public ActionResult Index(int page = 1, int pageSize = 5) {
    var totalItems = db.Items.Count();
    var items = db.Items.OrderBy(i => i.Id)
                        .Skip((page - 1) * pageSize)
                        .Take(pageSize)
                        .ToList();

    ViewBag.TotalPages = Math.Ceiling((double)totalItems / pageSize);
    return View(items);
}

This action retrieves the total number of items, calculates the number of items to skip based on the current page, and fetches only the required items for that page.

In your view, you would then render pagination controls based on the total number of pages calculated in the ViewBag:

@for (int i = 1; i <= ViewBag.TotalPages; i++) {
    <a href="?page=@i">@i</a>
}

Edge Cases & Gotchas

While implementing pagination, there are several edge cases and challenges to consider:

  • Empty Data Sets: Ensure that your pagination logic can handle cases where there are no items to display. This can be done by checking the total number of items and conditionally rendering pagination controls.
  • Out of Range Pages: Implement logic to handle requests for pages that exceed the total number of available pages. This can be done by redirecting to the last page or showing an error message.
  • Dynamic Data: If your data is dynamic, ensure that your pagination logic updates accordingly when items are added or removed. This may require re-fetching data or re-initializing pagination.

Performance & Best Practices

To optimize pagination performance, consider the following best practices:

  • Limit Data on Initial Load: For server-side pagination, always limit the amount of data fetched on initial load. This helps reduce server load and improves response times.
  • Asynchronous Loading: Implement AJAX calls to load data asynchronously when changing pages. This improves user experience as it avoids full page reloads.
  • Cache Results: Use caching strategies to store frequently accessed data. This can significantly reduce database load and improve response times.

Conclusion

In this article, we covered the implementation of pagination in ASP.NET MVC using jQuery. We explored both client-side and server-side pagination techniques, and discussed best practices to ensure optimal performance. Here are the key takeaways:

  • Pagination is essential for improving user experience in web applications.
  • Client-side pagination is suitable for smaller datasets, while server-side pagination is necessary for larger datasets.
  • Implementing pagination requires careful consideration of edge cases and performance optimizations.
  • Utilizing jQuery and AJAX can enhance the interactivity and responsiveness of pagination features.
Simple Pagination in AspNet MVC 2

S
Shubham Batra
Programming author at Code2Night β€” sharing tutorials on ASP.NET, C#, and more.
View all posts β†’

Related Articles

Using Ajax Beginform in Asp.Net MVC
Jun 18, 2023
MVC Crud Operation With Entity Framework
Sep 20, 2020
How to Import CSV in ASP.NET MVC
Feb 02, 2024
How to generate pdf using itextsharp in asp.net mvc
Aug 06, 2023
Previous in ASP.NET MVC
Authenticating User Login using custom filter in Asp.Net MVC
Next in ASP.NET MVC
Record Screen In asp.net MVC using RecordRTC

Comments

Contents

🎯

Interview Prep

Ace your ASP.NET MVC interview with curated Q&As for all levels.

View ASP.NET MVC Interview Q&As

More in ASP.NET MVC

  • Implement Stripe Payment Gateway In ASP.NET 58647 views
  • Jquery Full Calender Integrated With ASP.NET 39553 views
  • Microsoft Outlook Add Appointment and Get Appointment using … 27494 views
  • How to implement JWT Token Authentication and Validate JWT T… 25185 views
  • Payumoney Integration With Asp.Net MVC 23140 views
View all ASP.NET MVC posts β†’

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • Asp.net Core
  • C
  • C#
  • DotNet
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  Β·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor