PagingExtensions.cs :  » Bloggers » Mayando » JelleDruyts » Web » Mvc » Paging » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Bloggers » Mayando 
Mayando » JelleDruyts » Web » Mvc » Paging » PagingExtensions.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;

namespace JelleDruyts.Web.Mvc.Paging{
    /// <summary>
    /// Provides extension methods for paging.
    /// </summary>
    /// <remarks>
    /// Adapted from an early version of the paged list available at http://pagedlist.codeplex.com/.
    /// </remarks>
    public static class PagingExtensions
  {
    #region HtmlHelper extensions

        /// <summary>
        /// Returns the markup for a pager control.
        /// </summary>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="currentPage">The current page.</param>
        /// <param name="totalItemCount">The total item count.</param>
        /// <returns>The markup for a pager control.</returns>
    public static string Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount)
    {
      return Pager(htmlHelper, pageSize, currentPage, totalItemCount, null, null);
    }

        /// <summary>
        /// Returns the markup for a pager control.
        /// </summary>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="currentPage">The current page.</param>
        /// <param name="totalItemCount">The total item count.</param>
        /// <param name="actionName">The name of the action to link to.</param>
        /// <returns>The markup for a pager control.</returns>
        public static string Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, string actionName)
    {
      return Pager(htmlHelper, pageSize, currentPage, totalItemCount, actionName, null);
    }

        /// <summary>
        /// Returns the markup for a pager control.
        /// </summary>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="currentPage">The current page.</param>
        /// <param name="totalItemCount">The total item count.</param>
        /// <param name="values">The route values.</param>
        /// <returns>The markup for a pager control.</returns>
        public static string Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, object values)
    {
      return Pager(htmlHelper, pageSize, currentPage, totalItemCount, null, new RouteValueDictionary(values));
    }

        /// <summary>
        /// Returns the markup for a pager control.
        /// </summary>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="currentPage">The current page.</param>
        /// <param name="totalItemCount">The total item count.</param>
        /// <param name="actionName">The name of the action to link to.</param>
        /// <param name="values">The route values.</param>
        /// <returns>The markup for a pager control.</returns>
        public static string Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, string actionName, object values)
    {
      return Pager(htmlHelper, pageSize, currentPage, totalItemCount, actionName, new RouteValueDictionary(values));
    }

        /// <summary>
        /// Returns the markup for a pager control.
        /// </summary>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="currentPage">The current page.</param>
        /// <param name="totalItemCount">The total item count.</param>
        /// <param name="valuesDictionary">The route values dictionary.</param>
        /// <returns>The markup for a pager control.</returns>
        public static string Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, RouteValueDictionary valuesDictionary)
    {
      return Pager(htmlHelper, pageSize, currentPage, totalItemCount, null, valuesDictionary);
    }

        /// <summary>
        /// Returns the markup for a pager control.
        /// </summary>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="currentPage">The current page.</param>
        /// <param name="totalItemCount">The total item count.</param>
        /// <param name="actionName">The name of the action to link to.</param>
        /// <param name="valuesDictionary">The route values dictionary.</param>
        /// <returns>The markup for a pager control.</returns>
        public static string Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, string actionName, RouteValueDictionary valuesDictionary)
    {
      if (valuesDictionary == null)
      {
        valuesDictionary = new RouteValueDictionary();
      }
      if (actionName != null)
      {
        if (valuesDictionary.ContainsKey("action"))
        {
          throw new ArgumentException("The valuesDictionary already contains an action.", "actionName");
        }
        valuesDictionary.Add("action", actionName);
      }
      var pager = new Pager(htmlHelper.ViewContext, pageSize, currentPage, totalItemCount, valuesDictionary);
      return pager.RenderHtml();
    }

    #endregion

    #region IQueryable<T> extensions

        /// <summary>
        /// Creates a new <see cref="IPagedList&lt;T&gt;"/> for the specified query.
        /// </summary>
        /// <typeparam name="T">The type of the items in the list.</typeparam>
        /// <param name="source">The source query.</param>
        /// <param name="pageIndex">Index of the page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns>A paged list for the specified query.</returns>
    public static IPagedList<T> ToPagedList<T>(this IQueryable<T> source, int pageIndex, int pageSize)
    {
      return new PagedList<T>(source, pageIndex, pageSize);
    }

        /// <summary>
        /// Creates a new <see cref="IPagedList&lt;T&gt;"/> for the specified query.
        /// </summary>
        /// <typeparam name="T">The type of the items in the list.</typeparam>
        /// <param name="source">The source query.</param>
        /// <param name="pageIndex">Index of the page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalCount">The total count.</param>
        /// <returns>A paged list for the specified query.</returns>
        public static IPagedList<T> ToPagedList<T>(this IQueryable<T> source, int pageIndex, int pageSize, int totalCount)
    {
      return new PagedList<T>(source, pageIndex, pageSize, totalCount);
    }

    #endregion

    #region IEnumerable<T> extensions

        /// <summary>
        /// Creates a new <see cref="IPagedList&lt;T&gt;"/> for the specified list.
        /// </summary>
        /// <typeparam name="T">The type of the items in the list.</typeparam>
        /// <param name="source">The source items.</param>
        /// <param name="pageIndex">Index of the page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns>A paged list for the specified list.</returns>
        public static IPagedList<T> ToPagedList<T>(this IEnumerable<T> source, int pageIndex, int pageSize)
    {
      return new PagedList<T>(source, pageIndex, pageSize);
    }

        /// <summary>
        /// Creates a new <see cref="IPagedList&lt;T&gt;"/> for the specified list.
        /// </summary>
        /// <typeparam name="T">The type of the items in the list.</typeparam>
        /// <param name="source">The source items.</param>
        /// <param name="pageIndex">Index of the page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalCount">The total count.</param>
        /// <returns>A paged list for the specified list.</returns>
        public static IPagedList<T> ToPagedList<T>(this IEnumerable<T> source, int pageIndex, int pageSize, int totalCount)
    {
      return new PagedList<T>(source, pageIndex, pageSize, totalCount);
    }

    #endregion
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.