Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    APIs
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> MVC >> Paging in ASP.NET MVC3
Paging in ASP.NET MVC3
Paging in ASP.NET MVC3


by Awadhendra Tiwari on 4/20/2012 9:55:10 PM

Views: 4083       Comments: 0

Paging in ASP.NET MVC3

In this article I will tell you that how to create paging in ASP.NET MVC3. For creating Paging functionality I had created an extension method which we will directly use in our views. Here I will give you required steps to create Paging for your application.

Creating a ViewModel

Here I am creating a ViewModel named PageInfo which contains Paging information.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

 

namespace SportsStore.WebUI.Models

{

    public class PageInfo

    {

        public int TotalItems { get; set; }

        public int ItemsPerPage { get; set; }

        public int CurrentPage { get; set; }

 

        public int TotalPages

        {

            get

            {

                return (int)Math.Ceiling((decimal)TotalItems / ItemsPerPage);

            }

        }

    }

}

 

After creating PageInfo view model we need to create PagingHelper class. ‘Paging Helper’ class work as an extension method for ASP.NET MVC so that it needs to be static.

Creating PagingHelper extension method

Here I am creating PagingHelper extension method.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using SportsStore.WebUI.Models;

using System.Text;

 

namespace SportsStore.WebUI.HtmlHelpers

{

    public static class PagingHelpers

    {

        /// <summary>

        /// This method returns an MVCHtmlString which

        /// is used for Paging purpose. this represents

        /// that it is an extension method, PageInfo is a

        /// ViewModel class which I had created and pageUrl

        /// is anomyous type function.

        /// </summary>

        public static MvcHtmlString PageLinks(this HtmlHelper html, PageInfo pageInfo, Func<int, string> pageUrl)

        {

            //Create an object of StringBuilder class

            StringBuilder result = new StringBuilder();

 

            for (int i = 1; i <= pageInfo.TotalPages; i++)

            {

                //TagBuilder class is used to create Tag.

                //Here I am creating <a></a> tag.

                TagBuilder tag = new TagBuilder("a");

 

                //Merge required attributed like href, title etc.

                tag.MergeAttribute("href", pageUrl(i));

 

                //Set inner heml.

                tag.InnerHtml = i.ToString();

 

                if (i == pageInfo.CurrentPage)

                {

                    //Add any class if you want to add.

                    tag.AddCssClass("selected");

                }

 

                //Finally add resulted string in StringBuilder object.

                result.Append(tag.ToString());

            }

 

            //Create MVCHtmlString.

            return MvcHtmlString.Create(result.ToString());

        }

    }

}

 

Now I am going to use this extension method in our view to create Paging

Using PagingHelper extension method

<div class="pager">

    @Html.PageLinks(Model.PageInfo, x => Url.Action("List", new { page = x }))

</div>

 

Now after executing current application output will be as follows.

Paging in ASP.NET MVC3

Note: This is not complete example. Here I am trying to tell that how to use extension method to create HtmlHelper and how to create Paging. If you copy and paste above code then it will not work.

Thanks for reading this article. Please write your valuable comments.

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Awadhendra TiwariRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 6108
Advertisement
MindStick Cleaner
Advertise with Us
  
Copyright © 2009 - 2013MindStick. All Rights Reserved.