用mvc技术实现分页

分页的原理

“分页,是一种将所有数据分段展示给用户的技术.用户每次看到的不是全部数据,而是其中的一部分,如果在其中没有找到自习自己想要的内容,用户可以通过制定页码或是翻页的方式转换可见内容,直到找到自己想要的内容为止.其实这和我们阅读书籍很类似”、

下面是代码

@{
    ViewBag.Title = "Index";
}

<div style="display:flex;justify-content:space-between">
    <div class="btn-group">
        <button type="button" class="btn btn-default">新增</button>
        <button type="button" class="btn btn-default">删除</button>
    </div>
    <div class="input-group">
        <label>名称</label>
        <input type="text" id="txtCondName" value="@ViewBag.name" />
        <input type="button" value="搜索" id="btnSearch" />

    </div>
</div>
<table class="table table-bordered">
    <thead>
        <tr>
            <th>编号</th>
            <th>名称</th>
            <th>备注</th>
        </tr>
    </thead>

    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>@item.ID</td>
                <td>@item.Name</td>
                <td>@item.Remark</td>
            </tr>
        }

    </tbody>
</table>
<nav aria-label="Page navigation" style="display:flex;justify-content:space-between">
    <ul class="pagination">

        <li>共 @ViewBag.totpage 页,第
            <input type="text" id=""value="1" />
            页,每页显示
            <select id="pagesize">
                @{
                    var pagesize = new List<int> { 5, 10, 20, 50, 100 };
                }
                @foreach (var item in pagesize)
                {
                    if (ViewBag.pagesize == item)
                    {
                        <option value="@item" selected="selected">@item</option>
                    }
                    else
                    {
                        <option value="@item">@item</option>

                    }
                }

            </select>
            条</li>

    </ul>
    <ul class="pagination">

        <li><a href="javascript:page(1);">首页</a></li>
        @if (ViewBag.pageinex > 1)
        {
            <li><a href="javascript:page(@ViewBag.pageindex-1);">上页</a></li>
        }
        else
        {
            <li class="disabled"><a href="javascript:page(@ViewBag.pageindex-1);">上页</a></li>

        }
        @if (ViewBag.pageinex < ViewBag.totpage)
        {
            <li><a href="javascript:page(@ViewBag.pageindex+1);">下页</a></li>
        }
        else
        {
            <li class="disabled"><a href="javascript:page(@ViewBag.pageindex+1);">下页</a></li>
        }
        <li><a href="/role/index?pageindex=@ViewBag.pageindex">末页</a></li>
        <li><input type="button" value="go" onclick="page(3);" /></li>

    </ul>
</nav>

@section scripts
    {
    <script>
        function page(pageindex)
        {
            var pagesize = $("#pagesize").val();
            var name = $("#txtCondName").val();
            window.location.href = "/role/index?pageindex=" + pageindex +"&pageSize"+ pagesize + "&name=" + name;
        }
    </script>
    }
using fenye.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace fenye.Controllers
{
   
    public class RoleController : Controller
    {
        RbacDBEntities db = new RbacDBEntities();
        // GET: Role
        public ActionResult Index(int pageindex=1,int pageSize=10,string name="")
        {
            //总记录数满足条件
            var count = db.Roles.Where(p => p.Name.Contains(name)).Count();
            var totapage = Math.Ceiling(count * 1.00 / pageSize);

            //分页查询704
            var role = db.Roles.Where(p => p.Name.Contains(name))//条件
                .OrderBy(p => p.ID)//排序
                .Skip(pageSize * (pageindex - 1))//跳过前多少条
                .Take(pageSize).ToList();//取多少条
            ViewBag.pageindex = pageindex;
            ViewBag.totpage = totapage;
            ViewBag.name = name;
            ViewBag.pageSize = pageSize;
            return View(role);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值