An improved version of the ASP. NET MVC page implementation-Add the same view to set multiple pagination

Source: Internet
Author: User

I've already implemented ASP. NET MVC paging (view that blog post), but it has limitations and must ensure that only one page can be found in the same view, and if you need to set up multiple paging in the same view, but I'm not able to do that, I re-optimized the original code. Added more flexible configuration properties and generation rules to solve the above problem, the code is as follows:

First, PageInfo class


Using system;using system.collections.generic;using system.linq;using system.web; namespace ROIS. models{   ///<summary>   //pagination information    //</summary>    public class pageinfo    {        private int _recordcount = 0;        Private int _ PageSize = 10;        private int _currentpageno = 1;        private string _pagen Octrlname = "_pageno";        private bool _createscript = true;       //&LT;SU mmary>       //Get or set total records        //</summary>      &NB Sp public int recordcount        {            get      &NBSP ;     {                return _recordcount;        &NB Sp  }      &NBSP;     set            {                if (VA Lue > 0)                 {              &NBSP ;     _recordcount = value;               }        &N Bsp  }       }        //<summary>       /// Gets or sets the number of records per page        //</summary>        public int pagesize    &NB Sp   {            get            {      &NBSP ;         return _pagesize;           }            set            {                if (value &G T 0) &nbsP               {                    _p Agesize = value;               }           }       }        //<summary>       ///Get or set the current index page number (from 1 Start calculation)        //</summary>        public int currentpageno    &NB Sp   {            get            {      &NBSP ;         return _currentpageno;           }       &N Bsp     set            {                if (VA Lue > 0)                 {              &NBSP ;   &nbsp if (value > this. PageCount)                     {                        _currentpageno = this. pagecount;                   }                    else                    {    &NBS P                   _currentpageno = value;          & nbsp        }               }          & nbsp }       }        //<summary>       ///Get Total pages &n Bsp      //</summary>        public int pagecount        {&nbs P         &NBSP get            {                if (this. RecordCount <= 0)                 {            &NB Sp       return 1;               }         & nbsp       return this. Recordcount/this. PageSize + (this. RecordCount% this. PageSize > 0? 1:0);           }       }         // <summary>       //Get or set the name of the control that holds the page number        //</summary>  &nbsp ;     public string pagenoctrlname        {            get            {                return _pagenoctrlname;           }            set            {        &NBSP ;       if (!string. IsNullOrEmpty (value))               {            &N Bsp       _pagenoctrlname = value;               }           }       }        //<summary>    &NB Sp  //Whether you need to create a script function        //</summary>        public bool createscript& nbsp       {            get {return _createscript;}             set {_createscript = value;}        }         public PageInfo ()         {} &nbsp ;       public PageInfo (int RecordcounT, int currentpageno, int pageSize = ten, string pagenoctrlname = NULL, bool Createscript = true)       &NBSP ; {            this. RecordCount = recordcount;            this. PageSize = pagesize;            this. Currentpageno = currentpageno;            this. Pagenoctrlname = pagenoctrlname;            this. Createscript = createscript;       }         //&LT;SUMMARY&GT;&NBSP ;      //whether homepage        //</summary>       //<returns& gt;</returns>        public bool Isfirstpage ()         {            return (this. Currentpageno <= 1);       }         //<summary>  &nbs P    //Is last        //</summary>       //<returns></retur ns>        public bool Islastpage ()         {            Return (this. Currentpageno >= this. PageCount);       }    }}

Second, _pager partial view (recommended in shared directory)


@using ROIS. Models, @model pageinfo  @if (model!=null && model.recordcount > 0) {        &NBSP;&NBS P;<div class= "Pager" >    @ (Model.currentpageno) page &nbsp;/&nbsp; total @ (@Model. PageCount) page, @if ( Model.isfirstpage ()) {    <span>|&lt; &nbsp;&nbsp; page </span>    <span >&lt; prev </span>}else{    <a href= "javascript:turnpage (1," @Model. Pagenoctrlname ");" >|&lt; First &nbsp;&nbsp; page </a>    <a href= "Javascript:turnpage (@ ( model.currentpageno-1), "@Model. Pagenoctrlname"); " >&lt; prev </a>} @if (Model.islastpage ()) {    <span> next &gt;</span>    <span> End &nbsp;&nbsp; page &gt;|</span>}else{     <a href= "Javascript:turnpage (@ (model.currentpageno+1), "@Model. Pagenoctrlname"); " > Next &gt;</a>     <a href= "Javascript:turnpage (@Model. PageCount," @Model. pagenoctRlname ");" > End &nbsp;&nbsp; page &gt;|</a> go to: <select id= "pages" onchange= "Javascript:turnpage (This.value, "@Model. Pagenoctrlname"); " >    @for (int i = 1; I <= model.pagecount; i++)     {        if (Model.curre Ntpageno = = i)         {        <option value= "@i" selected= "selected" > @ (i) page </option>       }        else        {    &NB Sp   <option value= "@i" > @ (i) page </option>       }   }</select>< Input type= "hidden" id= "@Model. Pagenoctrlname" Name= "@Model. Pagenoctrlname"/></div>if (model.createscript ) {<script type= "text/javascript" ><!--    function turnpage (pageno,ctrlid) {         var Opageno = document.getElementById (ctrlid);        Opageno.value = pageno;  &nbs P     OPageNo.form.submit ();   }     function getform (obj) {        if (Obj.paren TNode.nodeName.toLowerCase () = = "Form") {            return obj.parentnode;    &NB Sp   Else {            GetForm (obj.parentnode);       }  &nbsp ; }//--></script>} }

Third, how to use: Background controller action to add:

String pageno = request.form["_pageno"];                int Ipageno = 1; Int.                TryParse (PageNo, out Ipageno); PageInfo PageInfo = new PageInfo (Detaillist.count (), Ipageno, +, "_pageno", false); Viewbag.pageinfo = PageInfo;
PageNo = request.form["_pageno2"];                Ipageno = 1; Int.                TryParse (PageNo, out Ipageno); PageInfo PageInfo2 = new PageInfo (Detaillist2.count (), Ipageno, +, "_pageno2", true);
Viewbag.pageinfo2 = PageInfo2;

The Front view page code is as follows: (Note: Rois is my project name, replace according to the actual situation)

@Html. Partial ("_pager", Viewbag.pageinfo as ROIS. Models.pageinfo) @Html. Partial ("_pager", Viewbag.pageinfo2 as ROIS. Models.pageinfo)

Article sync posted on my personal site: http://www.zuowenjun.cn/post/2014/10/23/65.html

An improved version of the ASP. NET MVC page implementation-Add the same view to set multiple pagination

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.