博客
关于我
ASP.NET MVC分页实现
阅读量:485 次
发布时间:2019-03-06

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

PageInfo类与基于POST的分页实现

作为一名开发人员,我在ASP.NET MVC项目中遇到了分页控件的不足之处,决定自己实现一个分页局部视图,并结合PageInfo类来满足需求。以下是详细的实现方案。

PageInfo类

为了实现分页功能,首先创建了一个PageInfo类,该类 encapsulates 分页的相关信息,包括记录总数、每页记录数、当前页码以及总页数等属性。以下是类的详细定义:

using System;using System.Collections.Generic;using System.Linq;using System.Web;

namespace ROIS.Models{public class PageInfo{private int _RecordCount = 0;private int _PageSize = 10;private int _CurrentPageNo = 1;

public int RecordCount    {        get { return _RecordCount; }        set         {            if (value > 0)            {                _RecordCount = value;            }        }    }    public int PageSize    {        get { return _PageSize; }        set         {            if (value > 0)            {                _PageSize = value;            }        }    }    public int CurrentPageNo    {        get { return _CurrentPageNo; }        set         {            if (value > 0)            {                if (value > this.PageCount)                {                    _CurrentPageNo = this.PageCount;                }                else                {                    _CurrentPageNo = value;                }            }        }    }    public int PageCount    {        get         {            if (this.RecordCount <= 0)            {                return 1;            }            return (this.RecordCount / this.PageSize) + (this.RecordCount % this.PageSize > 0 ? 1 : 0);        }    }    public PageInfo()    {    }    public PageInfo(int recordCount, int currentPageNo, int pageSize = 10)    {        this.RecordCount = recordCount;        this.PageSize = pageSize;        this.CurrentPageNo = currentPageNo;    }    public bool IsFirstPage()    {        return this.CurrentPageNo <= 1;    }    public bool IsLastPage()    {        return this.CurrentPageNo >= this.PageCount;    }}

}

《Pager》局部视图

为实现灵活的分页功能,开发了一个基于JavaScript的局部视图控制器《Pager》,可以在任意页面位置使用。该视图控制器支持分页操作,并与PageInfo类进行数据交互。以下是视图控制器的实现代码:

@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页面中,使用以下代码:

@using (Html.BeginForm()){// 数据列表HTML代码@Html.Partial("_Pager", ViewBag.PageInfo as ROIS.Models.PageInfo)}

通过以上方案,可以在任何页面任意位置实现灵活的分页功能。该方案基于POST分页方式,确保所有操作都在FORM中进行。如果需要基于URL的分页实现,可以在未来进行扩展。

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

你可能感兴趣的文章
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>