博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET MVC分页实现
阅读量:477 次
发布时间:2019-03-06

本文共 3888 字,大约阅读时间需要 12 分钟。

ASP.NET MVC中不能使用分页控件,所以我就自己写了一个分页局部视图,配合PageInfo类,即可实现在任何页面任意位置呈现分页,由于采用的是基于POST分页方式,所以唯一的限制就是必须放在FORM中,当然以后我会考虑实现基于URL分页的!

一、PageInfo类

1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Web;  5    6 namespace ROIS.Models  7 {  8     ///   9     /// 分页信息 10     ///  11     public class PageInfo 12     { 13         private int _RecordCount = 0; 14         private int _PageSize = 10; 15         private int _CurrentPageNo=1; 16   17         ///  18         /// 获取或设置记录总数 19         ///  20         public int RecordCount 21         { 22             get 23             { 24                 return _RecordCount; 25             } 26             set 27             { 28                 if (value > 0) 29                 { 30                     _RecordCount = value; 31                 } 32             } 33         } 34   35         ///  36         /// 获取或设置每页记录数 37         ///  38         public int PageSize 39         { 40             get { 41                 return _PageSize; 42             }  43             set { 44                 if (value > 0) 45                 { 46                     _PageSize = value; 47                 } 48             } 49         } 50   51         ///  52         /// 获取或设置当前索引页码(从1开始计算) 53         ///  54         public int CurrentPageNo 55         { 56             get { 57                 return _CurrentPageNo; 58             } 59   60             set { 61                 if (value > 0) 62                 { 63                     if (value > this.PageCount) 64                     { 65                         _CurrentPageNo = this.PageCount; 66                     } 67                     else 68                     { 69                         _CurrentPageNo = value; 70                     } 71                 } 72             }  73         } 74   75         ///  76         /// 获取总页数 77         ///  78         public int PageCount 79         { 80             get 81             { 82                 if (this.RecordCount <= 0) 83                 { 84                     return 1; 85                 } 86   87                 return this.RecordCount / this.PageSize + (this.RecordCount % this.PageSize > 0 ? 1 : 0); 88             } 89         } 90   91         public PageInfo() 92         { } 93   94         public PageInfo(int recordCount, int currentPageNo, int pageSize = 10) 95         { 96             this.RecordCount = recordCount; 97             this.PageSize = pageSize; 98             this.CurrentPageNo = currentPageNo; 99         }100  101  102         /// 103         /// 是否为首页104         /// 105         /// 
106 public bool IsFirstPage()107 {108 return (this.CurrentPageNo <= 1);109 }110 111 112 /// 113 /// 是否为末页114 /// 115 ///
116 public bool IsLastPage()117 {118 return (this.CurrentPageNo>=this.PageCount);119 }120 121 }122 }123

 

二、_Pager局部视图(建议放在Shared目录下)

@using ROIS.Models; @model PageInfo @if (Model!=null && Model.RecordCount > 0){           
第@(Model.CurrentPageNo) 页 / 共@(@Model.PageCount)页,@if (Model.IsFirstPage()){
|<首  页
<上一页}else{
|<首  页
<上一页}@if (Model.IsLastPage()){
下一页>
末  页>|}else{
下一页>
末  页>|}转到:
}

 

三、使用方法:

后台Controller的Action中加入:

            string pageNo = Request.Form["_pageno"];

            int iPageNo = 1;
            int.TryParse(pageNo, out iPageNo);
            PageInfo pageInfo=new PageInfo(5000,iPageNo, 20);

            ViewBag.PageInfo = pageInfo;

前台VIEW页面代码如下:(注: ROIS是我专案名称,依实际情况更换)

@using (Html.BeginForm())

{
      这里面是数据列表HTML代码

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

}

原文出自我的个人网站:

转载地址:http://otwdz.baihongyu.com/

你可能感兴趣的文章