ASP.NET MVC分頁實現之改進版-增加同一個視圖可設定多個分頁

來源:互聯網
上載者:User

標籤:style   http   color   io   os   ar   使用   java   for   

我之前就已經實現了ASP.NET MVC分頁(查看該博文),但它有局限性,必須確保在同一個視圖中只能有一處分頁,若需要在同一個視圖中設定多個分頁,卻無能為力,為此,我重新對原先的代碼進行了最佳化,增加了更為靈活的配置屬性及建置規則,解決了上述問題,代碼如下:

一、PageInfo類


using System;using System.Collections.Generic;using System.Linq;using System.Web; namespace ROIS.Models{    /// <summary>    /// 分頁資訊    /// </summary>    public class PageInfo    {        private int _RecordCount = 0;        private int _PageSize = 10;        private int _CurrentPageNo = 1;        private string _PageNoCtrlName = "_pageno";        private bool _CreateScript = true;        /// <summary>        /// 擷取或設定記錄總數        /// </summary>        public int RecordCount        {            get            {                return _RecordCount;            }            set            {                if (value > 0)                {                    _RecordCount = value;                }            }        }         /// <summary>        /// 擷取或設定每頁記錄數        /// </summary>        public int PageSize        {            get            {                return _PageSize;            }            set            {                if (value > 0)                {                    _PageSize = value;                }            }        }         /// <summary>        /// 擷取或設定當前索引頁碼(從1開始計算)        /// </summary>        public int CurrentPageNo        {            get            {                return _CurrentPageNo;            }             set            {                if (value > 0)                {                    if (value > this.PageCount)                    {                        _CurrentPageNo = this.PageCount;                    }                    else                    {                        _CurrentPageNo = value;                    }                }            }        }         /// <summary>        /// 擷取總頁數        /// </summary>        public int PageCount        {            get            {                if (this.RecordCount <= 0)                {                    return 1;                }                 return this.RecordCount / this.PageSize + (this.RecordCount % this.PageSize > 0 ? 1 : 0);            }        }          /// <summary>        /// 擷取或設定儲存頁碼的控制項名稱        /// </summary>        public string PageNoCtrlName        {            get            {                return _PageNoCtrlName;            }            set            {                if (!string.IsNullOrEmpty(value))                {                    _PageNoCtrlName = value;                }            }        }         /// <summary>        /// 是否需要建立指令碼函數        /// </summary>        public bool CreateScript        {            get { return _CreateScript; }            set { _CreateScript = value; }        }         public PageInfo()        { }         public PageInfo(int recordCount, int currentPageNo, int pageSize = 10, string pageNoCtrlName = null, bool createScript = true)        {            this.RecordCount = recordCount;            this.PageSize = pageSize;            this.CurrentPageNo = currentPageNo;            this.PageNoCtrlName = pageNoCtrlName;            this.CreateScript = createScript;        }          /// <summary>        /// 是否為首頁        /// </summary>        /// <returns></returns>        public bool IsFirstPage()        {            return (this.CurrentPageNo <= 1);        }          /// <summary>        /// 是否為末頁        /// </summary>        /// <returns></returns>        public bool IsLastPage()        {            return (this.CurrentPageNo >= this.PageCount);        }     }}

二、_Pager局部視圖(建議放在Shared目錄下)


@using ROIS.Models;@model PageInfo @if (Model!=null && Model.RecordCount > 0){           <div class="pager">    第@(Model.CurrentPageNo) 頁&nbsp;/&nbsp;共@(@Model.PageCount)頁,@if (Model.IsFirstPage()){    <span>|&lt;首&nbsp;&nbsp;頁</span>    <span>&lt;上一頁</span>}else{    <a href="javascript:turnPage(1,"@Model.PageNoCtrlName");">|&lt;首&nbsp;&nbsp;頁</a>    <a href="javascript:turnPage(@(Model.CurrentPageNo-1),"@Model.PageNoCtrlName");">&lt;上一頁</a>}@if (Model.IsLastPage()){    <span>下一頁&gt;</span>    <span>末&nbsp;&nbsp;頁&gt;|</span>}else{     <a href="javascript:turnPage(@(Model.CurrentPageNo+1),"@Model.PageNoCtrlName");">下一頁&gt;</a>     <a href="javascript:turnPage(@Model.PageCount,"@Model.PageNoCtrlName");">末&nbsp;&nbsp;頁&gt;|</a>}轉到:<select id="pages" onchange="javascript:turnPage(this.value,"@Model.PageNoCtrlName");">    @for (int i = 1; i <= Model.PageCount; i++)    {        if (Model.CurrentPageNo == i)        {        <option value="@i" selected="selected">第@(i)頁</option>        }        else        {        <option value="@i">第@(i)頁</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;        oPageNo.form.submit();    }     function getForm(obj) {        if (obj.parentNode.nodeName.toLowerCase() == "form") {            return obj.parentNode;        } else {            getForm(obj.parentNode);        }    }//--></script>} }

三、使用方法:後台Controller的Action中加入:

string pageNo = Request.Form["_pageno"];                int iPageNo = 1;                int.TryParse(pageNo, out iPageNo);                PageInfo pageInfo = new PageInfo(detailList.Count(), iPageNo, 30, "_pageno", false);ViewBag.PageInfo = pageInfo;
 pageNo = Request.Form["_pageno2"];                iPageNo = 1;                int.TryParse(pageNo, out iPageNo);                PageInfo pageInfo2 = new PageInfo(detailList2.Count(), iPageNo, 30, "_pageno2",true);
ViewBag.PageInfo2 = pageInfo2;

前台VIEW頁面代碼如下:(註: ROIS是我專案名稱,依實際情況更換)

@Html.Partial("_Pager", ViewBag.PageInfo as ROIS.Models.PageInfo) @Html.Partial("_Pager", ViewBag.PageInfo2 as ROIS.Models.PageInfo)  

文章同步發表於我的個人網站:http://www.zuowenjun.cn/post/2014/10/23/65.html

ASP.NET MVC分頁實現之改進版-增加同一個視圖可設定多個分頁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.