Super easy to use lightweight MVC paging controls JPager. Net, mvcjpager.net

Source: Internet
Author: User

Super easy to use lightweight MVC paging controls JPager. Net, mvcjpager.net

JPager. Net MVC is a lightweight paging control that you can't imagine.

JPager. Net MVC is a lightweight paging control that is easy to implement and easy to use.

JPager. Net MVC is a lightweight paging control that is easy to use. The Code has been carefully elaborated. It has been repeatedly suggested by many people to be modified, and is finally in use. It is very easy to share with you. Source code. First:

JPager. Net MVC lightweight paging control JPager. Net. dll core code

PagerInBase. cs

Namespace JPager. net {// <summary> /// page base class /// </summary> public class PagerInBase {/// <summary> // current page /// </ summary> public int PageIndex {get; set ;}//< summary> /// page number /// </summary> public int PageSize {get; set ;} // Skip the specified number of elements in the sequence: public int Skip => (PageIndex-1) * PageSize; /// <summary> /// request URL /// </summary> public string RequetUrl => System. web. httpContext. current. request. url. originalString; // <summary> // The constructor initializes the current page and page number. // </summary> public PagerInBase () {if (PageIndex = 0) pageIndex = 1; if (PageSize = 0) PageSize = 10 ;}}}

PagerResult. cs

Using System; using System. collections. generic; using System. text; namespace JPager. net {// <summary> // ULR Assembly // </summary> internal static class Exts {public static string GetUrl (this string url, int curIndex, int reps) {return url. replace ("pageindex =" + curIndex. toString (), "pageindex =" + reps. toString ());}} /// <summary> /// core paging code /// </summary> /// <typeparam name = "T"> </typeparam> public Class PagerResult <T> {public int Code {get; set;} public int Total {get; set;} public IEnumerable <T> DataList {get; set ;} public int PageSize {get; set;} public int PageIndex {get; set;} public string RequestUrl {get; set ;} /// <summary> /// page number Html /// </summary> /// <param name = "cssClass"> default style: jpager </param> /// <returns> </returns> public string PagerHtml (string cssClass = "jpager ") {If (PageIndex = 0) PageIndex = 1; if (RequestUrl. IndexOf ("? ", StringComparison. Ordinal) =-1) RequestUrl + = "? Pageindex = 1 "; else if (RequestUrl. IndexOf (" & pageindex ", StringComparison. Ordinal) =-1 & RequestUrl. IndexOf ("? Pageindex ", StringComparison. ordinal) =-1) RequestUrl + = "& pageindex = 1"; var html = new StringBuilder (); html. appendFormat ("<span class = '{0}'>", cssClass); var pageLen = Math. ceiling (double) Total/PageSize); html. appendFormat ("<a href = '{0}'> homepage </a>", RequestUrl. getUrl (PageIndex, 1); html. appendFormat ("<a href = '{0}'> previous page </a>", RequestUrl. getUrl (PageIndex, PageIndex <2? 1: PageIndex-1); var si = PageIndex <= 6? 1: PageIndex-5; var ei = si + 9; while (si <= pageLen & si <= ei) html. AppendFormat (si = PageIndex? "<A style = 'color: black; border: none; 'href = '{0}'> {1} </a> ": "<a href = '{0}'> {1} </a>", RequestUrl. getUrl (PageIndex, si), si ++); html. appendFormat ("<a href = '{0}'> next page </a>", RequestUrl. getUrl (PageIndex, (int) (PageIndex> pageLen-1? PageLen: PageIndex + 1); html. appendFormat ("<a href = '{0}'> last page </a>", Math. abs (Total) <= 0? RequestUrl. getUrl (PageIndex, 1): RequestUrl. getUrl (PageIndex, (int) pageLen); html. append (@ "</span>"); return html. toString ();}}}

Usage:

HomeController. cs

Using System; using System. collections. generic; using System. linq; using System. web. mvc; using JPager. net; using JPager. net. web. models; namespace JPager. net. web. controllers {public class HomeController: Controller {public ActionResult Index (UserParams param) {// The number of lines displayed on each page defaults to 10 // param. pageSize = 10; // Save the Search Condition ViewBag. searchName = param. name; ViewBag. searchAge = param. age; // obtain the data set var list = PageContent (); // Retrieve var query = param. Name according to the condition! = Null? List. where (t => t. name. contains (param. name )). toList (): list. toList (); // var data = query. skip (param. skip ). take (param. pageSize); // the total number of pages var count = query. count; var res = new PagerResult <User> {Code = 0, DataList = data, Total = count, PageSize = param. pageSize, PageIndex = param. pageIndex, RequestUrl = param. requetUrl}; return View (res);} // test data public List <User> PageContent () {var list = new List <User> (); for (var t = 0; t <10000; t ++) {list. add (new User {Id = t, Name = "Joye.net" + t. toString (), Age = t + 10, Score = t, Address =" http://yinrq.cnblogs.com/ ", AddTime = DateTime. Now});} return list ;}}}

Create User. cs and UserParams. cs in the Models folder

User. cs

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace JPager.Net.Web.Models{  public class UserParams:JPager.Net.PagerInBase  {    public int Id { get; set; }    public string Name { get; set; }    public int ? Age { get; set; }    public int Score { get; set; }    public string Address { get; set; }    public DateTime AddTime { get; set; }  }}

UserParams. cs

using System;namespace JPager.Net.Web.Models{  public class User  {    public int Id { get; set; }     public string Name { get; set; }    public int Age { get; set; }    public int Score { get; set; }    public string Address { get; set; }    public DateTime AddTime { get; set; }  }}

View display

@ Model JPager. net. pagerResult <JPager. net. web. models. user >@{ ViewBag. title = "Index" ;}< h2> JPager. net MVC lightweight paging Control 

Github: https://github.com/decadestory/JPager.Net

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.