信仰,就像恐惧和爱一样,是一种决定我们人生走向的力量。
实体,接口,实现类
public interface IPagedList
{
int PageCount { get ; }
int TotalItemCount { get ; }
int PageIndex { get ; }
int PageNumber { get ; }
int PageSize { get ; }
bool HasPreviousPage { get ; }
bool HasNextPage { get ; }
bool IsFirstPage { get ; }
bool IsLastPage { get ; }
int ItemStart { get ; }
int ItemEnd { get ; }
}
public interface IPagedList<out T> : IPagedList, IEnumerable<T>
{
}
public class PagedList<T> : List<T>, IPagedList<T>
{
public PagedList (IEnumerable<T> source, int index, int pageSize, int ? totalCount = null )
: this (source.AsQueryable(), index, pageSize, totalCount)
{
}
public PagedList (IQueryable<T> source, int index, int pageSize, int ? totalCount = null )
{
if (index < 0 )
throw new ArgumentOutOfRangeException("index" , "值不能低于0" );
if (pageSize < 1 )
throw new ArgumentOutOfRangeException("pageSize" , "值不能低于1." );
if (source == null )
source = new List<T>().AsQueryable();
int realTotalCount = source.Count();
PageSize = pageSize;
PageIndex = index;
TotalItemCount = totalCount.HasValue ? totalCount.Value : realTotalCount;
PageCount = TotalItemCount > 0 ? (int ) Math.Ceiling(TotalItemCount/(double ) PageSize) : 0 ;
HasPreviousPage = (PageIndex > 0 );
HasNextPage = (PageIndex < (PageCount - 1 ));
IsFirstPage = (PageIndex <= 0 );
IsLastPage = (PageIndex >= (PageCount - 1 ));
ItemStart = PageIndex*PageSize + 1 ;
ItemEnd = Math.Min(PageIndex*PageSize + PageSize, TotalItemCount);
if (TotalItemCount <= 0 )
return ;
if (realTotalCount != TotalItemCount)
AddRange(source.Take(PageSize));
else
AddRange(source.Skip(PageIndex*PageSize).Take(PageSize));
}
#region IPagedList Members
public int PageCount { get ; private set ; }
public int TotalItemCount { get ; private set ; }
public int PageIndex { get ; private set ; }
public int PageNumber
{
get { return PageIndex + 1 ; }
}
public int PageSize { get ; private set ; }
public bool HasPreviousPage { get ; private set ; }
public bool HasNextPage { get ; private set ; }
public bool IsFirstPage { get ; private set ; }
public bool IsLastPage { get ; private set ; }
public int ItemStart { get ; private set ; }
public int ItemEnd { get ; private set ; }
#endregion
}
public class Pager : IHtmlString
{
private readonly int currentPage;
private readonly HtmlHelper htmlHelper;
private readonly int pageSize;
protected readonly PagerOptions pagerOptions;
private int totalItemCount;
public Pager (HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount)
{
this .htmlHelper = htmlHelper;
this .pageSize = pageSize;
this .currentPage = currentPage;
this .totalItemCount = totalItemCount;
pagerOptions = new PagerOptions();
}
public virtual string ToHtmlString ()
{
PaginationModel model = BuildPaginationModel(GeneratePageUrl);
if (!String.IsNullOrEmpty(pagerOptions.DisplayTemplate))
{
string templatePath = string .Format("DisplayTemplates/{0}" , pagerOptions.DisplayTemplate);
return htmlHelper.Partial(templatePath, model).ToHtmlString();
}
else
{
var sb = new StringBuilder();
foreach (PaginationLink paginationLink in model.PaginationLinks)
{
if (paginationLink.Active)
{
if (paginationLink.IsCurrent)
{
sb.AppendFormat("<span class=\"current\">{0}</span>" , paginationLink.DisplayText);
}
else if (!paginationLink.PageIndex.HasValue)
{
sb.AppendFormat(paginationLink.DisplayText);
}
else
{
var linkBuilder = new StringBuilder("<a" );
if (pagerOptions.AjaxOptions != null )
foreach (var ajaxOption in pagerOptions.AjaxOptions.ToUnobtrusiveHtmlAttributes())
linkBuilder.AppendFormat(" {0}=\"{1}\"" , ajaxOption.Key, ajaxOption.Value);
linkBuilder.AppendFormat(" href=\"{0}\" title=\"{1}\">{2}</a>" , paginationLink.Url,
paginationLink.DisplayTitle, paginationLink.DisplayText);
sb.Append(linkBuilder.ToString());
}
}
else
{
if (!paginationLink.IsSpacer)
{
sb.AppendFormat("<span class=\"disabled\">{0}</span>" , paginationLink.DisplayText);
}
else
{
sb.AppendFormat("<span class=\"spacer\">{0}</span>" , paginationLink.DisplayText);
}
}
}
return sb.ToString();
}
}
public Pager Options (Action<PagerOptionsBuilder> buildOptions)
{
buildOptions(new PagerOptionsBuilder(pagerOptions));
return this ;
}
public virtual PaginationModel BuildPaginationModel (Func<int , string > generateUrl)
{
int pageCount;
if (pagerOptions.UseItemCountAsPageCount)
{
pageCount = totalItemCount;
totalItemCount = pageCount*pageSize;
}
else
{
pageCount = (int ) Math.Ceiling(totalItemCount/(double ) pageSize);
}
var model = new PaginationModel
{
PageSize = pageSize,
CurrentPage = currentPage,
TotalItemCount = totalItemCount,
PageCount = pageCount
};
if (pagerOptions.DisplayFirstPage)
{
model.PaginationLinks.Add(new PaginationLink
{
Active = (currentPage > 1 ? true : false ),
DisplayText = pagerOptions.FirstPageText,
DisplayTitle = pagerOptions.FirstPageTitle,
PageIndex = 1 ,
Url = generateUrl(1 )
});
}
if (!pagerOptions.HidePreviousAndNextPage)
{
string previousPageText = pagerOptions.PreviousPageText;
model.PaginationLinks.Add(currentPage > 1
? new PaginationLink
{
Active = true ,
DisplayText = previousPageText,
DisplayTitle = pagerOptions.PreviousPageTitle,
PageIndex = currentPage - 1 ,
Url = generateUrl(currentPage - 1 )
}
: new PaginationLink {Active = false , DisplayText = previousPageText});
}
int start = 1 ;
int end = pageCount;
int nrOfPagesToDisplay = pagerOptions.MaxNrOfPages;
if (pageCount > nrOfPagesToDisplay)
{
int middle = (int ) Math.Ceiling(nrOfPagesToDisplay/2 d) - 1 ;
int below = (currentPage - middle);
int above = (currentPage + middle);
if (below < 2 )
{
above = nrOfPagesToDisplay;
below = 1 ;
}
else if (above > (pageCount - 2 ))
{
above = pageCount;
below = (pageCount - nrOfPagesToDisplay + 1 );
}
start = below;
end = above;
}
if (start > 1 )
{
model.PaginationLinks.Add(new PaginationLink
{
Active = true ,
PageIndex = 1 ,
DisplayText = "1" ,
Url = generateUrl(1 )
});
if (start > 3 )
{
model.PaginationLinks.Add(new PaginationLink
{
Active = true ,
PageIndex = 2 ,
DisplayText = "2" ,
Url = generateUrl(2 )
});
}
if (start > 2 )
{
model.PaginationLinks.Add(new PaginationLink {Active = false , DisplayText = "..." , IsSpacer = true });
}
}
for (int i = start; i <= end; i++)
{
if (i == currentPage || (currentPage <= 0 && i == 1 ))
{
model.PaginationLinks.Add(new PaginationLink
{
Active = true ,
PageIndex = i,
IsCurrent = true ,
DisplayText = i.ToString()
});
}
else
{
model.PaginationLinks.Add(new PaginationLink
{
Active = true ,
PageIndex = i,
DisplayText = i.ToString(),
Url = generateUrl(i)
});
}
}
if (end < pageCount)
{
if (end < pageCount - 1 )
{
model.PaginationLinks.Add(new PaginationLink {Active = false , DisplayText = "..." , IsSpacer = true });
}
if (end < pageCount - 2 )
{
model.PaginationLinks.Add(new PaginationLink
{
Active = true ,
PageIndex = pageCount,
DisplayText = (pageCount).ToString(),
Url = generateUrl(pageCount)
});
}
}
if (!pagerOptions.HidePreviousAndNextPage)
{
string nextPageText = pagerOptions.NextPageText;
model.PaginationLinks.Add(currentPage < pageCount
? new PaginationLink
{
Active = true ,
PageIndex = currentPage + 1 ,
DisplayText = nextPageText,
DisplayTitle = pagerOptions.NextPageTitle,
Url = generateUrl(currentPage + 1 )
}
: new PaginationLink {Active = false , DisplayText = nextPageText});
}
if (pagerOptions.DisplayLastPage)
{
model.PaginationLinks.Add(new PaginationLink
{
Active = (currentPage < pageCount ? true : false ),
DisplayText = pagerOptions.LastPageText,
DisplayTitle = pagerOptions.LastPageTitle,
PageIndex = pageCount,
Url = generateUrl(pageCount)
});
}
if (pagerOptions.AjaxOptions != null )
{
model.AjaxOptions = pagerOptions.AjaxOptions;
}
model.Options = pagerOptions;
return model;
}
protected virtual string GeneratePageUrl (int pageNumber)
{
ViewContext viewContext = htmlHelper.ViewContext;
RouteValueDictionary routeDataValues = viewContext.RequestContext.RouteData.Values;
RouteValueDictionary pageLinkValueDictionary;
if (pageNumber == 1 && !pagerOptions.AlwaysAddFirstPageNumber)
{
pageLinkValueDictionary = new RouteValueDictionary(pagerOptions.RouteValues);
if (routeDataValues.ContainsKey(pagerOptions.PageRouteValueKey))
{
routeDataValues.Remove(pagerOptions.PageRouteValueKey);
}
}
else
{
pageLinkValueDictionary = new RouteValueDictionary(pagerOptions.RouteValues)
{
{pagerOptions.PageRouteValueKey, pageNumber}
};
}
if (!pageLinkValueDictionary.ContainsKey("controller" ) && routeDataValues.ContainsKey("controller" ))
{
pageLinkValueDictionary.Add("controller" , routeDataValues["controller" ]);
}
if (!pageLinkValueDictionary.ContainsKey("action" ) && routeDataValues.ContainsKey("action" ))
{
pageLinkValueDictionary.Add("action" , routeDataValues["action" ]);
}
pageLinkValueDictionary = pageLinkValueDictionary.FixListRouteDataValues();
VirtualPathData virtualPathForArea = RouteTable.Routes.GetVirtualPathForArea(viewContext.RequestContext,
pageLinkValueDictionary);
return virtualPathForArea == null ? null : virtualPathForArea.VirtualPath;
}
}
public class Pager<TModel> : Pager
{
private HtmlHelper<TModel> htmlHelper;
public Pager (HtmlHelper<TModel> htmlHelper, int pageSize, int currentPage, int totalItemCount)
: base (htmlHelper, pageSize, currentPage, totalItemCount)
{
this .htmlHelper = htmlHelper;
}
public Pager<TModel> Options (Action<PagerOptionsBuilder<TModel>> buildOptions)
{
buildOptions(new PagerOptionsBuilder<TModel>(pagerOptions, htmlHelper));
return this ;
}
}
using System.Web.Mvc.Ajax;
using System.Web.Routing;
namespace MvcPaging
{
public class PagerOptions
{
public PagerOptions ()
{
RouteValues = new RouteValueDictionary();
DisplayTemplate = Defaults.DisplayTemplate;
MaxNrOfPages = Defaults.MaxNrOfPages;
AlwaysAddFirstPageNumber = Defaults.AlwaysAddFirstPageNumber;
PageRouteValueKey = Defaults.DefaultPageRouteValueKey;
PreviousPageText = DefaultDefaults.PreviousPageText;
PreviousPageTitle = DefaultDefaults.PreviousPageTitle;
NextPageText = DefaultDefaults.NextPageText;
NextPageTitle = DefaultDefaults.NextPageTitle;
FirstPageText = DefaultDefaults.FirstPageText;
FirstPageTitle = DefaultDefaults.FirstPageTitle;
LastPageText = DefaultDefaults.LastPageText;
LastPageTitle = DefaultDefaults.LastPageTitle;
DisplayFirstPage = DefaultDefaults.DisplayFirstPage;
DisplayLastPage = DefaultDefaults.DisplayLastPage;
UseItemCountAsPageCount = DefaultDefaults.UseItemCountAsPageCount;
HidePreviousAndNextPage = DefaultDefaults.HidePreviousAndNextPage;
}
public RouteValueDictionary RouteValues { get ; internal set ; }
public string DisplayTemplate { get ; internal set ; }
public int MaxNrOfPages { get ; internal set ; }
public AjaxOptions AjaxOptions { get ; internal set ; }
public bool AlwaysAddFirstPageNumber { get ; internal set ; }
public string Action { get ; internal set ; }
public string PageRouteValueKey { get ; set ; }
public string PreviousPageText { get ; set ; }
public string PreviousPageTitle { get ; set ; }
public string NextPageText { get ; set ; }
public string NextPageTitle { get ; set ; }
public string FirstPageText { get ; set ; }
public string FirstPageTitle { get ; set ; }
public string LastPageText { get ; set ; }
public string LastPageTitle { get ; set ; }
public bool DisplayFirstAndLastPage
{
get { return DisplayFirstPage && DisplayLastPage; }
}
public bool DisplayFirstPage { get ; set ; }
public bool DisplayLastPage { get ; set ; }
public bool HidePreviousAndNextPage { get ; internal set ; }
public bool UseItemCountAsPageCount { get ; internal set ; }
public static class DefaultDefaults
{
public const int MaxNrOfPages = 10 ;
public const string DisplayTemplate = null ;
public const bool AlwaysAddFirstPageNumber = false ;
public const string DefaultPageRouteValueKey = "page" ;
public const string PreviousPageText = "«" ;
public const string PreviousPageTitle = "Previous page" ;
public const string NextPageText = "»" ;
public const string NextPageTitle = "Next page" ;
public const string FirstPageText = "<" ;
public const string FirstPageTitle = "First page" ;
public const string LastPageText = ">" ;
public const string LastPageTitle = "Last page" ;
public const bool DisplayFirstPage = false ;
public const bool DisplayLastPage = false ;
public const bool UseItemCountAsPageCount = false ;
public static bool HidePreviousAndNextPage = false ;
}
public static class Defaults
{
public static int MaxNrOfPages = DefaultDefaults.MaxNrOfPages;
public static string DisplayTemplate = DefaultDefaults.DisplayTemplate;
public static bool AlwaysAddFirstPageNumber = DefaultDefaults.AlwaysAddFirstPageNumber;
public static string DefaultPageRouteValueKey = DefaultDefaults.DefaultPageRouteValueKey;
public static string PreviousPageText = DefaultDefaults.PreviousPageText;
public static string PreviousPageTitle = DefaultDefaults.PreviousPageTitle;
public static string NextPageText = DefaultDefaults.NextPageText;
public static string NextPageTitle = DefaultDefaults.NextPageTitle;
public static string FirstPageText = DefaultDefaults.FirstPageText;
public static string FirstPageTitle = DefaultDefaults.FirstPageTitle;
public static string LastPageText = DefaultDefaults.LastPageText;
public static string LastPageTitle = DefaultDefaults.LastPageTitle;
public static bool DisplayFirstPage = DefaultDefaults.DisplayFirstPage;
public static bool DisplayLastPage = DefaultDefaults.DisplayLastPage;
public static bool UseItemCountAsPageCount = DefaultDefaults.UseItemCountAsPageCount;
public static void Reset ()
{
MaxNrOfPages = DefaultDefaults.MaxNrOfPages;
DisplayTemplate = DefaultDefaults.DisplayTemplate;
AlwaysAddFirstPageNumber = DefaultDefaults.AlwaysAddFirstPageNumber;
DefaultPageRouteValueKey = DefaultDefaults.DefaultPageRouteValueKey;
PreviousPageText = DefaultDefaults.PreviousPageText;
PreviousPageTitle = DefaultDefaults.PreviousPageTitle;
NextPageText = DefaultDefaults.NextPageText;
NextPageTitle = DefaultDefaults.NextPageTitle;
FirstPageText = DefaultDefaults.FirstPageText;
FirstPageTitle = DefaultDefaults.FirstPageTitle;
LastPageText = DefaultDefaults.LastPageText;
LastPageTitle = DefaultDefaults.LastPageTitle;
DisplayFirstPage = DefaultDefaults.DisplayFirstPage;
DisplayLastPage = DefaultDefaults.DisplayLastPage;
UseItemCountAsPageCount = DefaultDefaults.UseItemCountAsPageCount;
}
}
}
}
using System;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Web.Routing;
namespace MvcPaging
{
public class PagerOptionsBuilder
{
protected PagerOptions pagerOptions;
public PagerOptionsBuilder (PagerOptions pagerOptions)
{
this .pagerOptions = pagerOptions;
}
public PagerOptionsBuilder Action (string action)
{
if (action != null )
{
if (pagerOptions.RouteValues.ContainsKey("action" ))
{
throw new ArgumentException("The valuesDictionary already contains an action." , "action" );
}
pagerOptions.RouteValues.Add("action" , action);
pagerOptions.Action = action;
}
return this ;
}
public PagerOptionsBuilder AddRouteValue (string name, object value )
{
pagerOptions.RouteValues[name] = value ;
return this ;
}
public PagerOptionsBuilder SetPreviousPageText (string previousPageText)
{
pagerOptions.PreviousPageText = previousPageText;
return this ;
}
public PagerOptionsBuilder SetPreviousPageTitle (string previousPageTitle)
{
pagerOptions.PreviousPageTitle = previousPageTitle;
return this ;
}
public PagerOptionsBuilder SetNextPageText (string nextPageText)
{
pagerOptions.NextPageText = nextPageText;
return this ;
}
public PagerOptionsBuilder SetNextPageTitle (string nextPageTitle)
{
pagerOptions.NextPageTitle = nextPageTitle;
return this ;
}
public PagerOptionsBuilder SetFirstPageText (string firstPageText)
{
pagerOptions.FirstPageText = firstPageText;
return this ;
}
public PagerOptionsBuilder SetFirstPageTitle (string firstPageTitle)
{
pagerOptions.FirstPageTitle = firstPageTitle;
return this ;
}
public PagerOptionsBuilder SetLastPageText (string lastPageText)
{
pagerOptions.LastPageText = lastPageText;
return this ;
}
public PagerOptionsBuilder SetLastPageTitle (string lastPageTitle)
{
pagerOptions.LastPageTitle = lastPageTitle;
return this ;
}
public PagerOptionsBuilder DisplayFirstAndLastPage ()
{
pagerOptions.DisplayFirstPage = true ;
pagerOptions.DisplayLastPage = true ;
return this ;
}
public PagerOptionsBuilder DisplayFirstPage ()
{
pagerOptions.DisplayFirstPage = true ;
return this ;
}
public PagerOptionsBuilder DisplayLastPage ()
{
pagerOptions.DisplayLastPage = true ;
return this ;
}
public PagerOptionsBuilder HidePreviousAndNextPage ()
{
pagerOptions.HidePreviousAndNextPage = true ;
return this ;
}
public PagerOptionsBuilder RouteValues (object routeValues)
{
RouteValues(new RouteValueDictionary(routeValues));
return this ;
}
public PagerOptionsBuilder RouteValues (RouteValueDictionary routeValues)
{
if (routeValues == null )
{
throw new ArgumentException("routeValues may not be null" , "routeValues" );
}
pagerOptions.RouteValues = routeValues;
if (!string .IsNullOrWhiteSpace(pagerOptions.Action) && !pagerOptions.RouteValues.ContainsKey("action" ))
{
pagerOptions.RouteValues.Add("action" , pagerOptions.Action);
}
return this ;
}
public PagerOptionsBuilder DisplayTemplate (string displayTemplate)
{
pagerOptions.DisplayTemplate = displayTemplate;
return this ;
}
public PagerOptionsBuilder MaxNrOfPages (int maxNrOfPages)
{
pagerOptions.MaxNrOfPages = maxNrOfPages;
return this ;
}
public PagerOptionsBuilder AlwaysAddFirstPageNumber ()
{
pagerOptions.AlwaysAddFirstPageNumber = true ;
return this ;
}
public PagerOptionsBuilder PageRouteValueKey (string pageRouteValueKey)
{
if (pageRouteValueKey == null )
{
throw new ArgumentException("pageRouteValueKey may not be null" , "pageRouteValueKey" );
}
pagerOptions.PageRouteValueKey = pageRouteValueKey;
return this ;
}
public PagerOptionsBuilder UseItemCountAsPageCount ()
{
pagerOptions.UseItemCountAsPageCount = true ;
return this ;
}
internal PagerOptionsBuilder AjaxOptions (AjaxOptions ajaxOptions)
{
pagerOptions.AjaxOptions = ajaxOptions;
return this ;
}
}
public class PagerOptionsBuilder<TModel> : PagerOptionsBuilder
{
private readonly HtmlHelper<TModel> htmlHelper;
public PagerOptionsBuilder (PagerOptions pagerOptions, HtmlHelper<TModel> htmlHelper)
: base (pagerOptions)
{
this .htmlHelper = htmlHelper;
}
public PagerOptionsBuilder<TModel> AddRouteValueFor<TProperty>(Expression<Func<TModel, TProperty>> expression)
{
string name = ExpressionHelper.GetExpressionText(expression);
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
AddRouteValue(name, metadata.Model);
return this ;
}
public new PagerOptionsBuilder<TModel> Action (string action)
{
base .Action(action);
return this ;
}
public new PagerOptionsBuilder<TModel> AddRouteValue (string name, object value )
{
base .AddRouteValue(name, value );
return this ;
}
public new PagerOptionsBuilder<TModel> RouteValues (object routeValues)
{
base .RouteValues(routeValues);
return this ;
}
public new PagerOptionsBuilder<TModel> RouteValues (RouteValueDictionary routeValues)
{
base .RouteValues(routeValues);
return this ;
}
public new PagerOptionsBuilder<TModel> DisplayTemplate (string displayTemplate)
{
base .DisplayTemplate(displayTemplate);
return this ;
}
public new PagerOptionsBuilder<TModel> MaxNrOfPages (int maxNrOfPages)
{
base .MaxNrOfPages(maxNrOfPages);
return this ;
}
public new PagerOptionsBuilder<TModel> AlwaysAddFirstPageNumber ()
{
base .AlwaysAddFirstPageNumber();
return this ;
}
public new PagerOptionsBuilder<TModel> PageRouteValueKey (string pageRouteValueKey)
{
if (pageRouteValueKey == null )
{
throw new ArgumentException("pageRouteValueKey may not be null" , "pageRouteValueKey" );
}
pagerOptions.PageRouteValueKey = pageRouteValueKey;
return this ;
}
public new PagerOptionsBuilder<TModel> SetPreviousPageText (string previousPageText)
{
base .SetPreviousPageText(previousPageText);
return this ;
}
public new PagerOptionsBuilder<TModel> SetPreviousPageTitle (string previousPageTitle)
{
base .SetPreviousPageTitle(previousPageTitle);
return this ;
}
public new PagerOptionsBuilder<TModel> SetNextPageText (string nextPageText)
{
base .SetNextPageText(nextPageText);
return this ;
}
public new PagerOptionsBuilder<TModel> SetNextPageTitle (string nextPageTitle)
{
base .SetNextPageTitle(nextPageTitle);
return this ;
}
public new PagerOptionsBuilder<TModel> SetFirstPageText (string firstPageText)
{
base .SetFirstPageText(firstPageText);
return this ;
}
public new PagerOptionsBuilder<TModel> SetFirstPageTitle (string firstPageTitle)
{
base .SetFirstPageTitle(firstPageTitle);
return this ;
}
public new PagerOptionsBuilder<TModel> SetLastPageText (string lastPageText)
{
base .SetLastPageText(lastPageText);
return this ;
}
public new PagerOptionsBuilder<TModel> SetLastPageTitle (string lastPageTitle)
{
base .SetLastPageTitle(lastPageTitle);
return this ;
}
public new PagerOptionsBuilder<TModel> DisplayFirstAndLastPage ()
{
base .DisplayFirstAndLastPage();
return this ;
}
public new PagerOptionsBuilder<TModel> UseItemCountAsPageCount ()
{
base .UseItemCountAsPageCount();
return this ;
}
}
}
using System.Collections.Generic;
using System.Web.Mvc.Ajax;
namespace MvcPaging
{
public class PaginationModel
{
public PaginationModel ()
{
PaginationLinks = new List<PaginationLink>();
AjaxOptions = null ;
Options = null ;
}
public int PageSize { get ; internal set ; }
public int CurrentPage { get ; internal set ; }
public int PageCount { get ; internal set ; }
public int TotalItemCount { get ; internal set ; }
public IList<PaginationLink> PaginationLinks { get ; private set ; }
public AjaxOptions AjaxOptions { get ; internal set ; }
public PagerOptions Options { get ; internal set ; }
}
public class PaginationLink
{
public bool Active { get ; set ; }
public bool IsCurrent { get ; set ; }
public int ? PageIndex { get ; set ; }
public string DisplayText { get ; set ; }
public string DisplayTitle { get ; set ; }
public string Url { get ; set ; }
public bool IsSpacer { get ; set ; }
}
}
PagingExtensions.cs
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
namespace MvcPaging
{
public static class PagingExtensions
{
#region HtmlHelper extensions
public static Pager Pager (this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount)
{
return new Pager(htmlHelper, pageSize, currentPage, totalItemCount);
}
public static Pager Pager (this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount,
AjaxOptions ajaxOptions)
{
return new Pager(htmlHelper, pageSize, currentPage, totalItemCount).Options(o => o.AjaxOptions(ajaxOptions));
}
public static Pager<TModel> Pager<TModel>(this HtmlHelper<TModel> htmlHelper, int pageSize, int currentPage,
int totalItemCount)
{
return new Pager<TModel>(htmlHelper, pageSize, currentPage, totalItemCount);
}
public static Pager<TModel> Pager<TModel>(this HtmlHelper<TModel> htmlHelper, int pageSize, int currentPage,
int totalItemCount, AjaxOptions ajaxOptions)
{
return
new Pager<TModel>(htmlHelper, pageSize, currentPage, totalItemCount).Options(
o => o.AjaxOptions(ajaxOptions));
}
#endregion
#region IQueryable<T> extensions
public static IPagedList<T> ToPagedList<T>(this IQueryable<T> source, int pageIndex, int pageSize,
int ? totalCount = null )
{
return new PagedList<T>(source, pageIndex, pageSize, totalCount);
}
#endregion
#region IEnumerable<T> extensions
public static IPagedList<T> ToPagedList<T>(this IEnumerable<T> source, int pageIndex, int pageSize,
int ? totalCount = null )
{
return new PagedList<T>(source, pageIndex, pageSize, totalCount);
}
#endregion
}
}
RouteValueDictionaryExtensions.cs
using System.Collections;
using System.Web.Routing;
namespace MvcPaging
{
public static class RouteValueDictionaryExtensions
{
public static RouteValueDictionary FixListRouteDataValues (this RouteValueDictionary routes)
{
var newRv = new RouteValueDictionary();
foreach (string key in routes.Keys)
{
object value = routes[key];
if (value is IEnumerable && !(value is string ))
{
int index = 0 ;
foreach (object val in (IEnumerable) value )
{
newRv.Add(string .Format("{0}[{1}]" , key, index), val);
index++;
}
}
else
{
newRv.Add(key, value );
}
}
return newRv;
}
}
}