MVC学习以及研究
ASP.NET.MVC4 高級編程學習資料
25M?PDF?507頁 進入下載 ?點擊Professional.ASP.NET.MVC
?
andrewdavey-NotFoundMvc? 鏈接
martijnboland-MvcPaging? 鏈接
IPagedList.cs
PagedList.cs using System; using System.Collections.Generic; using System.Linq; namespace MvcPaging { 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", "Value can not be below 0."); if (pageSize < 1) throw new ArgumentOutOfRangeException("pageSize", "Value can not be less than 1."); if (source == null) source = new List<T>().AsQueryable(); var 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; var realTotalPages = (int)Math.Ceiling(realTotalCount / (double)PageSize); if (realTotalCount < TotalItemCount && realTotalPages <= PageIndex) AddRange(source.Skip((realTotalPages - 1) * PageSize).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 } }
Pager.cs
PagingExtensions.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Ajax; using System.Web.Routing; namespace MvcPaging { public static class PagingExtensions { #region AjaxHelper extensions public static HtmlString Pager(this AjaxHelper ajaxHelper, int pageSize, int currentPage, int totalItemCount, AjaxOptions ajaxOptions) { return Pager(ajaxHelper, pageSize, currentPage, totalItemCount, null, null, ajaxOptions); } public static HtmlString Pager(this AjaxHelper ajaxHelper, int pageSize, int currentPage, int totalItemCount, string actionName, AjaxOptions ajaxOptions) { return Pager(ajaxHelper, pageSize, currentPage, totalItemCount, actionName, null, ajaxOptions); } public static HtmlString Pager(this AjaxHelper ajaxHelper, int pageSize, int currentPage, int totalItemCount, object values, AjaxOptions ajaxOptions) { return Pager(ajaxHelper, pageSize, currentPage, totalItemCount, null, new RouteValueDictionary(values), ajaxOptions); } public static HtmlString Pager(this AjaxHelper ajaxHelper, int pageSize, int currentPage, int totalItemCount, string actionName, object values, AjaxOptions ajaxOptions) { return Pager(ajaxHelper, pageSize, currentPage, totalItemCount, actionName, new RouteValueDictionary(values), ajaxOptions); } public static HtmlString Pager(this AjaxHelper ajaxHelper, int pageSize, int currentPage, int totalItemCount, RouteValueDictionary valuesDictionary, AjaxOptions ajaxOptions) { return Pager(ajaxHelper, pageSize, currentPage, totalItemCount, null, valuesDictionary, ajaxOptions); } public static HtmlString Pager(this AjaxHelper ajaxHelper, int pageSize, int currentPage, int totalItemCount, string actionName, RouteValueDictionary valuesDictionary, AjaxOptions ajaxOptions) { 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(ajaxHelper.ViewContext, pageSize, currentPage, totalItemCount, valuesDictionary, ajaxOptions); return pager.RenderHtml(); } #endregion #region HtmlHelper extensions public static HtmlString Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount) { return Pager(htmlHelper, pageSize, currentPage, totalItemCount, null, null); } public static HtmlString Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, string actionName) { return Pager(htmlHelper, pageSize, currentPage, totalItemCount, actionName, null); } public static HtmlString Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, object values) { return Pager(htmlHelper, pageSize, currentPage, totalItemCount, null, new RouteValueDictionary(values)); } public static HtmlString Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, string actionName, object values) { return Pager(htmlHelper, pageSize, currentPage, totalItemCount, actionName, new RouteValueDictionary(values)); } public static HtmlString Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, RouteValueDictionary valuesDictionary) { return Pager(htmlHelper, pageSize, currentPage, totalItemCount, null, valuesDictionary); } public static HtmlString 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, null); return pager.RenderHtml(); } #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 } }
轉載于:https://www.cnblogs.com/zengxiangzhan/archive/2012/03/30/2426109.html
總結
- 上一篇: MySQL数据库备份和还原的常用命令
- 下一篇: 3.3.2 Rhino与env.js