mvc 分页PagedList简单使用

1.nuget下载PagedList包

 

2.PageListHelper类:

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

namespace ClothMvcApp.Helper
{
    public class PageListHelper<T>
    {
        public IPagedList<T> PageList { get; set; }

        public string Controller { get; set; }

        public string Action { get; set; }

        public string Key { get; set; }
    }
}

3.创建一个部分页_PagedListPartial.cshtml

<div class="page-box">
    @{

    int pageIndex = 1;
    int pageSize = 1;

    if (Model.PageList != null)
    {
        pageIndex = Model.PageList.PageNumber;

        pageSize = Model.PageList.PageSize;

        <span><span style="color:orange;font-weight:bold;">@Model.PageList.TotalItemCount</span>&nbsp;&nbsp;<span style="color: orange; font-weight: bold;">@Model.PageList.PageCount</span></span>

             <a href="@Url.Action(Model.Action, Model.Controller, new { pageIndex = 1,pageSize=Model.PageList.PageSize,key=Model.Key })">首页</a>

            if (Model.PageList.HasPreviousPage)
            {
                <a href="@Url.Action(Model.Action, Model.Controller, new { pageIndex = (Model.PageList.PageNumber - 1), pageSize = Model.PageList.PageSize, key = Model.Key })">上一页</a>
            }
            
             <select onchange="changePageSize(this)" style="width:70px;">
                @{
                    for (int i = 1; i <= 10; i++)
                    {
                        <option value="@(i*5)" @(Model.PageList.PageSize == i*5 ? "selected" : "")>@(i*5)</option>
                    }
                }
            </select>
        

            if (Model.PageList.HasNextPage)
            {
                <a href="@Url.Action(Model.Action, Model.Controller, new { pageIndex = (Model.PageList.PageNumber + 1), pageSize = Model.PageList.PageSize, key = Model.Key })">下一页</a>
            }


            <a href="@Url.Action(Model.Action, Model.Controller, new { pageIndex = Model.PageList.PageCount, pageSize = Model.PageList.PageSize, key = Model.Key })">末页</a>
        }
    }
  
</div>

<script type="text/javascript">

    function changePageSize(obj)
    {
        var pageSize = obj.value;
        window.location = "/Admin/@Model.Controller/@Model.Action?pageIndex=@pageIndex&pageSize="+pageSize+"&key=@Model.Key"

    }

    function search() {
        var key = document.getElementById("key").value;
        window.location = "/Admin/@Model.Controller/@Model.Action?pageIndex=1&pageSize=@pageSize&key=" + key;

    }

</script>

4.Index页面调用:

@using ClothMvcApp.EF;
@using ClothMvcApp.Helper;
@{
    Layout = null;
    var list = ViewBag.data as PagedList.IPagedList<ClothMvcApp.EF.Product>;
}
 @Html.Partial("_PagedListPartial", new PageListHelper<ClothMvcApp.EF.Product>() { Controller = "Cloth", Action = "Index", Key = ViewBag.key, PageList = list })

 

转载于:https://www.cnblogs.com/zoro-zero/p/5680971.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值