English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

asp.net MVC Pagination Code Sharing

This article shares the MVC pagination code for everyone's reference, and the specific content is as follows

using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using System.Data.Objects.DataClasses;
namespace System.Web.Mvc

 public static class PagerHelper
 
  /// <summary>
  /// Pagination
  /// </summary>
  /// <param name="helper"></param>
  /// <param name="id">Page ID</param>/param>
  /// <param name="currentPageIndex">Current Page</param>/param>
  /// <param name="pageSize">Page Size</param>/param>
  /// <param name="recordCount">Total Record Count</param>/param>
  /// <param name="htmlAttributes">Page header tag attributes</param>
  /// <param name="className">Pagination Style</param>
  /// <param name="mode">Pagination Mode</param>
  /// <returns></returns>
  public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, object htmlAttributes, string className, PageMode mode)
  
   TagBuilder builder = new TagBuilder("table");
   builder.IdAttributeDotReplacement = "_";
   builder.GenerateId(id);
   builder.AddCssClass(className);
   builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
   builder.InnerHtml = GetNormalPage(currentPageIndex, pageSize, recordCount, mode);
   return builder.ToString();
  }
  /// <summary>
  /// Pagination
  /// </summary>
  /// <param name="helper"></param>
  /// <param name="id">Page ID</param>/param>
  /// <param name="currentPageIndex">Current Page</param>/param>
  /// <param name="pageSize">Page Size</param>/param>
  /// <param name="recordCount">Total Record Count</param>/param>
  /// <param name="className">Pagination Style</param>
  /// <returns></returns>
  public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, string className)
  
   return Pager(helper, id, currentPageIndex, pageSize, recordCount, null, className, PageMode.Normal);
  }
  /// <summary>
  /// Pagination
  /// </summary>
  /// <param name="helper"></param>
  /// <param name="id">Page ID</param>/param>
  /// <param name="currentPageIndex">Current Page</param>/param>
  /// <param name="pageSize">Page Size</param>/param>
  /// <param name="recordCount">Total Record Count</param>/param>
  /// <returns></returns>
  public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount)
  
   return Pager(helper, id, currentPageIndex, pageSize, recordCount, null);
  }
  /// <summary>
  /// Pagination
  /// </summary>
  /// <param name="helper"></param>
  /// <param name="id">Page ID</param>/param>
  /// <param name="currentPageIndex">Current Page</param>/param>
  /// <param name="pageSize">Page Size</param>/param>
  /// <param name="recordCount">Total Record Count</param>/param>
  /// <param name="mode">Pagination Mode</param>
  /// <returns></returns>
  public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, PageMode mode)
  
   return Pager(helper, id, currentPageIndex, pageSize, recordCount, null, mode);
  }
  /// <summary>
  /// Pagination
  /// </summary>
  /// <param name="helper"></param>
  /// <param name="id">Page ID</param>/param>
  /// <param name="currentPageIndex">Current Page</param>/param>
  /// <param name="pageSize">Page Size</param>/param>
  /// <param name="recordCount">Total Record Count</param>/param>
  /// <param name="className">Pagination Style</param>
  /// <param name="mode">Pagination Mode</param>
  /// <returns></returns>
  public static string Pager(this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, string className, PageMode mode)
  
   return Pager(helper, id, currentPageIndex, pageSize, recordCount, null, className, mode);
  }
  /// <summary>
  /// Get Normal Pagination
  /// </summary>
  /// <param name="currentPageIndex"></param>
  /// <param name="pageSize"></param>
  /// <param name="recordCount"></param>
  /// <returns></returns>
  private static string GetNormalPage(int currentPageIndex, int pageSize, int recordCount, PageMode mode)
  
   int pageCount = (recordCount % pageSize == 0 &63; recordCount / pageSize : recordCount / pageSize + 1;
   StringBuilder url = new StringBuilder();
   url.Append(HttpContext.Current.Request.Url.AbsolutePath + "?page={0}");
   NameValueCollection collection = HttpContext.Current.Request.QueryString;
   string[] keys = collection.AllKeys;
   for (int i = 0; i < keys.Length; i++)
   
    if (keys[i].ToLower() != "page")
     url.AppendFormat("&{0}=");1}", keys[i], collection[keys[i]]);
   }
   StringBuilder sb = new StringBuilder();
   sb.Append("<tr><td>");
   sb.AppendFormat("Total {0} records, in total {1} page, current {2page "{0}", recordCount, pageCount, currentPageIndex);
   if (currentPageIndex == 1)
    sb.Append("<span>First page</span> ");
   else
   
    string url1 = string.Format(url.ToString(), 1;
    sb.AppendFormat("<span><a href={0}>First page</a></span> ", url1;
   }
   if (currentPageIndex > 1)
   
    string url1 = string.Format(url.ToString(), currentPageIndex - 1;
    sb.AppendFormat("<span><a href={0}>Previous page</a></span> ", url1;
   }
   else
    sb.Append("<span>Previous page</span> ");
   if (mode == PageMode.Numeric)
    sb.Append(GetNumericPage(currentPageIndex, pageSize, recordCount, pageCount, url.ToString()));
   if (currentPageIndex < pageCount)
   
    string url1 = string.Format(url.ToString(), currentPageIndex + 1;
    sb.AppendFormat("<span><a href={0}>Next page</a></span> ", url1;
   }
   else
    sb.Append("<span>Next page</span> ");
   if (currentPageIndex == pageCount)
    sb.Append("<span>Last page</span> ");
   else
   
    string url1 = string.Format(url.ToString(), pageCount);
    sb.AppendFormat("<span><a href={0}>Last page</a></span> ", url1;
   }
   return sb.ToString();
  }
  /// <summary>
  /// Get numeric pagination
  /// </summary>
  /// <param name="currentPageIndex"></param>
  /// <param name="pageSize"></param>
  /// <param name="recordCount"></param>
  /// <param name="pageCount"></param>
  /// <param name="url"></param>
  /// <returns></returns>
  private static string GetNumericPage(int currentPageIndex, int pageSize, int recordCount, int pageCount, string url)
  
   int k = currentPageIndex / 10;
   int m = currentPageIndex % 10;
   StringBuilder sb = new StringBuilder();
   if (currentPageIndex / 10 == pageCount / 10)
   
    if (m == 0)
    
     k--;
     m = 10;
    }
    else
     m = pageCount % 10;
   }
   else
    m = 10;
   for (int i = k * 10 + 1; i <= k * 10 + m; i++)
   
    if (i == currentPageIndex)
     sb.AppendFormat("<span><font color=red><b>{0}</b></font></span> ", i);
    else
    
     string url1 = string.Format(url.ToString(), i);
     sb.AppendFormat("<span><a href={0}>{1}<//a></span> ", url1, i);
    }
   }
   return sb.ToString();
  }
 }
 /// <summary>
 /// Pagination mode
 /// </summary>
 public enum PageMode
 
  /// <summary>
  /// Normal pagination mode
  /// </summary>
  Normal,
  /// <summary>
  /// Normal pagination with numeric pagination
  /// </summary>
  Numeric
 }
}

HTML code

<div id="pageNav" class="pageinator">
  @Html.ShowPageNavigate((int)ViewData["pageindex"], (int)ViewBag.pageSize, (int)ViewBag.totalCount);
 </div>

Controller

shopEntities shop = new shopEntities();
  public ActionResult Index()
  
   //10).Take(10;
   //ViewData["order"] = order;
   //return View();
   int pageIndex = Request["pageIndex"] == null ?63; 1 : int.Parse(Request["pageIndex"]);
   int pageSize = Request["pageSize"] == null ?63; 10 : int.Parse(Request["pageSize"]);
   int totalCount = 0;
   //Pass the pagination parameter data to the front-end.
   ViewData["pageIndex"] = pageIndex;
   //ViewData["pageSize"] = pageSize;
   ViewBag.pageSize = pageSize;
   //Total number of items
   totalCount = shop.tbl_order.Count();
   ViewBag.totalCount = totalCount;
   //Send the current page data to the front-end.
   //ViewData.Model = db.UserInfo.ToList();
   //List<tbl_order> pp = shop.tbl_order
   //     .OrderBy(u => u.id)
   //     .Skip((pageIndex - 1) * pageSize)
   //     .Take(pageSize).ToList();
   IQueryable<tbl_order> pp = shop.tbl_order
       .OrderBy(u => u.id)
       .Skip((pageIndex - 1) * pageSize)
       .Take(pageSize);
   return View(pp);
  } 

That's all for this article, I hope it will be helpful to everyone's learning, and I also hope everyone will support the Yelling Tutorial more.

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously, and this website does not own the copyright, has not been manually edited, and does not bear relevant legal liabilities. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please send an email to codebox.com (replace # with @ when sending an email) to report violations, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.

You May Also Like