ASP.NET MVC程序实现真分页(案例详细,值得收藏)

需要用到的sql语句

第一条SQL:查询总数,返回一个数字(总记录数)

select count(*) from 表名

第二条SQL:分页语句

select top 每页数据条数 * from 表名 where id not in (select top ((当前页数-1)*每页数据条数) id from 表名)

二.编码

1.1 编写SQL脚本

create database Paging
go
use Paging
go
create table Catgory
(

id int primary key identity,
name varchar(20),
)
go
create table Article
(
id int primary key identity,
title nvarchar(20),
author nvarchar(10),
price float,
cid int foreign key references Catgory(id)
)
go

insert into Catgory values
('学术'),('八卦'),('娱乐')
go
insert into Article values
('文章1','张三',11,1),
('文章2','张三',12,1),
('文章3','张三',13,3),
('文章4','张三',14,1),
('文章5','张三',15,2),
('文章6','张三',16,1),
('文章7','张三',17,3),
('文章8','张三',18,1),
('文章9','张三',19,1),
('文章10','张三',20,2),
('文章11','张三',21,2),
('文章12','张三',22,3),
('文章13','张三',23,1),
('文章14','张三',24,3),
('文章15','张三',25,3),
('文章16','张三',26,1),
('文章17','张三',27,1),
('文章18','张三',28,2),
('文章19','张三',29,3),
('文章20','张三',30,2)
go
select * from Article
select * from Catgory



-- select top 每页数据条数 * from 表名 where id not in ( select top ((当前页数-1)*每页数据条数) id from 表名)



select top 5 * from Article where id not in (select top ((2-1)* 5) id  from Article)

1.创建工程,添加bootstrap模板应用

构建 _Layout.cshtml

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <meta charset="utf-8" />

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>分页查询案例</title>

    @Styles.Render("~/Content/css")

    @Scripts.Render("~/bundles/modernizr")

</head>

<body>

    <div class="container body-content">

        @RenderBody()

    </div>

    @Scripts.Render("~/bundles/jquery")

    @Scripts.Render("~/bundles/bootstrap")

    @RenderSection("scripts", required: false)

</body>

</html>

2.使用布局页

3.导入模型

4.编写控制器

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

namespace Demo1.Controllers

{

    public class PageController : Controller

    {

        // GET: Pag

        public ActionResult Index(int pageIndex = 1, int pageSize = 3)

        {

            using (PagingEntities db = new PagingEntities())

            {

                var articles = db.Article.ToList();

                var res = db.Article.Include("Catgory")

                    .OrderBy(p => p.id)  

                    .Skip((pageIndex - 1) * pageSize)  //跳过前n个结果,返回剩余的结果。

                    .Take(pageSize)  //从查询结果中提取前n个结果

                    .ToList();

                ViewBag.pageIndex = pageIndex;

                ViewBag.pageSize = pageSize;

                //计算总数

                ViewBag.totalRows = articles.Count;

                //计算共有多少页

                ViewBag.totalPage = Math.Ceiling(articles.Count * 1.0 / pageSize);

                return View(res);

            }

        }

    }

}

4.编写页面代码

<div>

    <table class="table table-striped">

        <thead>

            <tr style="background-color:burlywood">

                <td>编号</td>

                <td>名称</td>

                <td>作者</td>

                <td>价格</td>

                <td>类型</td>

            </tr>

        </thead>

        <tbody>

            @foreach (var item in Model)

            {

                <tr>

                    <td>@item.id</td>

                    <td>@item.title</td>

                    <td>@item.author</td>

                    <td>@item.price</td>

                    <td>@item.Catgory.name</td>

                </tr>

            }

        </tbody>

    </table>

    <div>

        <nav aria-label="Page navigation example" style="display:flex;justify-content:space-between;">

            <ul class="pagination">

                <li class="page-item"><a class="page-link" href="javascript:page(1)">首页</a></li>

                <li class="page-item"><a class="page-link" href="javascript:page(@(ViewBag.pageIndex-1))">上一页</a></li>

                <li class="page-item"><a class="page-link" href="#">@ViewBag.pageIndex</a></li>

                <li class="page-item"><a class="page-link" href="javascript:page(@(ViewBag.pageIndex+1))">下一页</a></li>

                <li class="page-item"><a class="page-link" href="javascript:page(@ViewBag.totalPage)">尾页</a></li>

            </ul>

            <div>

                 @ViewBag.totalRows 条数据,

                每页显示

                <select id="pageSize" onchange="page()">

                    <option value="2">2</option>

                    <option value="5">5</option>

                    <option value="10">10</option>

                    <option value="15">15</option>

                    <option value="20">20</option>

                </select>

                条数据,

                前往第 <input type="number" id="txtPageIndex" name="name" value="@ViewBag.pageIndex" style="width:50px;" />

                <button id="go" class="btn">GO</button>

            </div>

        </nav>

    </div>

</div>

@section scripts{

    <script>

        $(function () {

            $("#pageSize").val(@ViewBag.pageSize)

        })

        function page(pageIndex) {

            if (pageIndex < 1) {

                alert("已经是第一页");

                return;

            }

            if (pageIndex > @ViewBag.totalPage) {

                alert("没有下一下页");

                return;

            }

            let pageSize=$("#pageSize").val()

            window.location.href = "/Page/index?pageIndex=" + pageIndex+"&pageSize="+pageSize

        }

        $("#go").click(function () {

            let pageIndex = $("#txtPageIndex").val();

            page(pageIndex)

        })

    </script>

}

  • 12
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Code_徐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值