asp.net 分页

asp.net 分页

控制

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Com.Hi.FY.Models;

namespace Com.Hi.FY.Controllers
{
    public class RoleController : Controller
    {
        // GET: Role
        RbacDBEntities db = new RbacDBEntities();
        public ActionResult Index(int pageindex=1,int pagesize=10,string Name="")   //pageindex 页数  //pagesize 每页显示多少条
        {
            //总记录数满足条件
            var counts = db.Roles.Where(p => p.Name.Contains(Name)).Count();
            var totalPage = Math.Ceiling(counts * 1.00 / pagesize);
            ViewBag.totalPage = totalPage;

            //根据页码的条数查询
            var a = db.Roles.Where(p => p.Name.Contains(Name))
                .OrderBy(p=>p.ID)                      //排序
                .Skip((pageindex - 1) * pagesize)     //跳过
                .Take(pagesize)                       //取多少条
                .ToList();                            //转换为集合
            ViewBag.pageindex = pageindex;            //记录页数
            ViewBag.Name = Name;                      //记录名字
            ViewBag.pagesize = pagesize;              //记录每页显示多少条

            return View(a);
        }
    }
}

视图


@{
    ViewBag.Title = "Index";
}

@using Com.Hi.FY.Models
@model List<Role>

<div style="display:flex; justify-content:space-between">
    <div class="btn-group" style="float:left">
        <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="txtCongName" value="@ViewBag.Name"/>
        <input type="button" value="搜索" id="btnSearch" onclick="page(1);" />
    </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><a href="#">10页,第
            <input type="text" value="1" id="pageindex" />页,每页显示
            <select id="pagesize" onchange="page(1)">
                @{
                    var pageindexs = new List<int> { 5, 10, 20, 50 };
                    foreach (var item in pageindexs)
                    {
                        if (ViewBag.pagesize == item)
                        {
                            <option value="@item" selected="selected">@item</option>
                        }
                        else
                        {
                            <option value="@item">@item</option>

                        }
                    }
                }


            </select></a></li>
    </ul>
    <ul class="pagination">
        <li><a href="javascript:page(1)">首页</a></li>             @*调脚本 javescript*@
        @if (@ViewBag.pageindex > 1)
        {
            <li><a href="javascript:page(@ViewBag.pageindex-1)">上页</a></li>
        }
        else
        {
            <li><a href="javascript:page(@ViewBag.pageindex-1)" class="disabled">上页</a></li>

        }

        <li><a href="javascript:page(@ViewBag.pageindex+1)">下页</a></li>
        <li><a href="javascript:page(@ViewBag.totalPage)">尾页</a></li>
        <li><input type="button" value="go" onclick="go();" /></li>

    </ul>
</nav>

@section scripts{
    <script>
        function page(pageindex) {
            var pageSize = $("#pagesize").val();
            var name = $("#txtCongName").val();
            window.location.href = "/role/index?pageindex=" + pageindex + "&pagesize=" + pageSize + "&name=" + name;    /*location 网页地址*/
        }
        function go() {
            //获取pageindex
            var pageindex = $("#pageindex").val();
            page(pageindex);
        }
    </script>   
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

南墙已撞

你的鼓励就是我最大的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值