Click here to Skip to main content
15,881,281 members
Articles / Web Development / ASP.NET

Pagination Class for ASP.NET MVC

Rate me:
Please Sign up or sign in to vote.
4.38/5 (17 votes)
15 Aug 2008CPOL3 min read 104.7K   6.4K   37  
A class for creating different types of pagination link in ASP.NET MVC.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PaginationInAspNetMvc.Models;
using Valentica.Libraries;

namespace PaginationInAspNetMvc.Controllers
{
    public class UsersController : Controller
    {
        public ActionResult Index()
        {
            // Add action logic here
            throw new NotImplementedException();
        }

        public ActionResult List()
        {
            string pageString = "";

            try
            {
                pageString = Request.Url.Segments[3];
            }
            catch (Exception)
            {
                pageString = null;
            }

            int page = (String.IsNullOrEmpty(pageString)) ? 1 : Int32.Parse(pageString);

            UserModel userModel = new UserModel();

            int totalUsers = userModel.GetTotalUsers();

            Pagination pagination = new Pagination(true);

            pagination.BaseUrl      = "/Users/List/";
            pagination.TotalRows    = totalUsers;
            pagination.CurPage      = page;
            pagination.PerPage      = 10;

            pagination.PrevLink = "Prev";
            pagination.NextLink = "Next";

            string pageLinks = pagination.GetPageLinks();

            int start   = (page - 1) * pagination.PerPage;
            int offset  = pagination.PerPage;

            List<User> users = userModel.GetUsers(start, offset);

            ViewData["title"]       = "Pagination in Asp.Net Mvc";
            ViewData["totalUsers"]  = totalUsers;
            ViewData["users"]       = users;
            ViewData["pageLinks"]   = pageLinks;

            return View();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Bangladesh Bangladesh
Software Engineer, Bangladesh.

Comments and Discussions