ASP.NET MVC 分页之 局部视图

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Web;

namespace MvcAppPager.Models
{
    public interface IPageOfList
    {
        long CurrentStart { get; }
        int PageIndex { get; set; }
        int PageSize { get; set; }
        int PageTotal { get; }
        long RecordTotal { get; set; }
    }

    public interface IPageOfList<T> : IPageOfList, IList<T>
    {

    }
    public class PageOfList<T>:List<T>,IList<T>,IPageOfList,IPageOfList<T>
    {
        public PageOfList(IEnumerable<T> items, int pageIndex, int pageSize, long recordTotal)
        {
            if (items!=null)
                AddRange(items);
            PageIndex = pageIndex;
            PageSize = pageSize;
            RecordTotal = recordTotal;
        }

        public PageOfList(int pageSize)
        {
            if (pageSize <= 0)
            {
                throw new ArgumentException("页面数据量必须大于0", "页面数据量");
            }
        }
        public int PageIndex { get; set; }
        public int PageSize { get; set; }

        public int PageTotal
        {
            get
            {
                //RecordTotal / PageSize  获取能够被布满的页面数,(RecordTotal % PageSize > 0 ? 1 : 0)判断是否有未布满的页面。
                return (int)RecordTotal / PageSize + (RecordTotal % PageSize > 0 ? 1 : 0);
            }
        }

        public long RecordTotal { get; set; }
        /// <summary>
        /// 当前页面的记录开始位置
        /// </summary>
        public long CurrentStart
        {
            get { return PageIndex * PageSize + 1; }
        }
        /// <summary>
        /// 当前页面的结束位置
        /// </summary>
        public long CurrentEnd
        {
            get { return (PageIndex + 1) * PageSize > RecordTotal ? RecordTotal : (PageIndex + 1) * PageSize; }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcAppPager.Models
{
    public class Order
    {
        public int ID { get; set; }
        public string OrderNo { get; set; }
        public decimal WayFee { get; set; }
        public string EMS { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcAppPager.Models;

namespace MvcAppPager.Controllers
{
    public class HomeController : Controller
    {
        List<Order> list=new List<Order>
        {
            new Order{ID=1,OrderNo="2016050501",WayFee = 20,EMS = "C01111"},
            new Order{ID=2,OrderNo="2016050502",WayFee = 20,EMS = "C01112"},
            new Order{ID=3,OrderNo="2016050503",WayFee = 20,EMS = "C01113"},
            new Order{ID=4,OrderNo="2016050504",WayFee = 20,EMS = "C01114"},
            new Order{ID=5,OrderNo="2016050505",WayFee = 20,EMS = "C01115"},
            new Order{ID=6,OrderNo="2016050506",WayFee = 20,EMS = "C01116"},
        };

        private const int PageSize = 2;

        private int counts;

        //
        // GET: /Home/
        public ActionResult Index(int pageIndex=0)
        {
            counts = list.Count;
            list = list.Skip(PageSize * pageIndex).Take(PageSize).ToList();
            PageOfList<Order> _ordersList=new PageOfList<Order>(list,pageIndex,PageSize,counts);
            return View(_ordersList);
        }

    }
}
@model MvcAppPager.Models.IPageOfList
@Styles.Render("~/Content/page.css")
<div class="fenye"><span>共 @Model.RecordTotal 条 记录,每页 @Model.PageSize 条  </span>
    @{
        System.Web.Routing.RouteValueDictionary route = new System.Web.Routing.RouteValueDictionary();
        foreach (var key in Html.ViewContext.RouteData.Values.Keys)
        {
            route[key] = Html.ViewContext.RouteData.Values[key];
        }

        foreach (string key in Html.ViewContext.RequestContext.HttpContext.Request.QueryString)
        {
            route[key] = Html.ViewContext.RequestContext.HttpContext.Request.QueryString[key];
        }
        if (Model.PageIndex <= 0)
        {
            <a class="backpage" href="javascript:void(0);">上一页</a>
        }
        else
        {
            route["pageIndex"] = Model.PageIndex - 1;
            Html.ActionLink("上一页", route["action"].ToString(), route).ToHtmlString();
        }

        if (Model.PageIndex > 3)
        {
            route["pageIndex"] = 0;
            Html.ActionLink("<b>1</b>", route["action"].ToString(), route).ToHtmlString().Replace("<", "<").Replace(">", ">");

            if (Model.PageIndex >= 5)
            {
                <a href=‘#‘>..</a>;
            }
        }

        for (int i = Model.PageIndex - 2; i <= Model.PageIndex; i++)
        {
            if (i < 1)
            {continue;}
            route["pageIndex"] = i - 1;
            @Html.ActionLink(i.ToString(), route["action"].ToString(), route["controller"]);

        }

        <a class=‘active‘ href=‘#‘><b> @(Model.PageIndex+1) </b></a>
        for (var i = Model.PageIndex + 2; i <= Model.PageIndex + 4; i++)
        {
            if (i > Model.PageTotal)
            {continue;}
            else{
                route["pageIndex"] = i - 1;
                @Html.ActionLink(i.ToString(), route["action"].ToString(), route);
            }
        }

        if (Model.PageIndex < Model.PageTotal - 4)
        {
            if (Model.PageIndex <= Model.PageTotal - 6)
            {
                <a href=‘#‘>..</a>
            }
            route["pageIndex"] = Model.PageTotal - 1;
            @Html.ActionLink(Model.PageTotal.ToString(), route["action"].ToString(), route).ToHtmlString();
        }
        if (Model.PageIndex < Model.PageTotal - 1)
        {
            route["pageIndex"] = Model.PageIndex + 1;
            Html.ActionLink("下一页", route["action"].ToString(), route).ToHtmlString();
        }
        else
        {
            <a class="nextpage" href="javascript:void(0);">下一页</a>
        }
    }
</div>

  

@model MvcAppPager.Models.PageOfList<MvcAppPager.Models.Order>
@{
    Layout = null;
    ViewBag.Title = "Index";
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    @Styles.Render("~/Content/page.css")
</head>
<body>
    <div id="body" style="width: 400px;float: left">
        @using (Html.BeginForm("Index","Home",FormMethod.Get))
        {
            <table>
                <tr>
                    <th>ID</th>
                    <th>订单号</th>
                    <th>运单号</th>
                    <th>运费</th>
                </tr>
                @if (Model != null && Model.Count > 0)
                {
                    foreach (var item in Model.ToList())
                    {
                        <tr>
                            <td>@item.ID</td>
                            <td>@item.OrderNo</td>
                            <td>@item.EMS</td>
                            <td>@item.WayFee</td>
                        </tr>
                    }
                }
            </table>
            Html.RenderPartial("pager", Model);
        }
    </div>
</body>
</html>
.fenye {
     float: right;
}

.fenye a,.fenye span,.fenye select {
    display: block;
    float: left;
    margin: 0 2px;
}

.fenye a {
    background: #fff;
    border: 1px solid #d6d6d6;
    color: #6f6f6f;
    height: 22px;
    line-height: 22px;
    margin: 0 2px;
    padding: 0 0 0 8px;
    position: relative;
}

.fenye a.nextpage:hover {
    background: #fff;
    border: 1px solid #ba191b;
    color: #000;
}

.fenye a.nextpage {
    height: 22px;
    margin: 0 0 0 2px;
    padding: 0px 5px;
}

.fenye a.backpage {
    background: #fff;
    border: 1px solid #d6d6d6;
    height: 22px;
    margin: 0 2px 0 0;
    padding: 0px 5px;
}

.fenye a.backpage:hover {
    background: url("images/pagebut.gif") no-repeat scroll left top rgba(0, 0, 0, 0);
    color: #000;
}

.fenye a:hover,.fenye a.active {
    background: #c32325;
    border: 1px solid #ba191b;
    color: #ffffff;
    text-decoration: none;
}

.fenye a.shenlue {
    background: none repeat scroll 0 0 rgba(0, 0, 0, 0);
    margin: 0 5px;
    padding: 0;
    border: none;
}

.fenye a.shenlue:hover {
    color: #333333;
}

.fenye a b {
    display: block;
    font-size: 12px;
    font-weight: normal;
    height: 22px;
    line-height: 22px;
    margin-right: 0;
    padding: 0 8px 0 0;
    position: relative;
}

原文地址:https://www.cnblogs.com/bjxingch/p/8193326.html

时间: 2024-07-28 22:28:09

ASP.NET MVC 分页之 局部视图的相关文章

ASP.NET MVC分页实现之改进版-增加同一个视图可设置多个分页

我之前就已经实现了ASP.NET MVC分页(查看该博文),但它有局限性,必须确保在同一个视图中只能有一处分页,若需要在同一个视图中设置多个分页,却无能为力,为此,我重新对原先的代码进行了优化,增加了更为灵活的配置属性及生成规则,解决了上述问题,代码如下: 一.PageInfo类 using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace ROIS.Models {

ASP.NET MVC 分页MvcPager

ASP.NET MVC 分页MvcPager 开源框架  admin  8个月前 (08-20)  1484浏览 他连续12年获得微软MPV称号,几年前,他写的ASP.NET分页控件,被很多.NET开发人员使用,现在他又写了名为MvcPager的分页扩展,并免费开源,支持MVC通用分页,与EF完美结合,支持AJAX分页,简单灵活,提供多个演示案例,是迄今为止最好的MVC分页方式,推荐各位ASP.NET MVC开发者使用. 分页是每个项目必须面对的技术点,不好的分页不但体验不好,而且会影响系统的整

Asp.Net MVC 分页、检索、排序整体实现

原文:Asp.Net MVC 分页.检索.排序整体实现 很多时候需要这样的功能,对表格进行分页.排序和检索.这个有很多实现的方式,有现成的表格控件.用前端的mvvm,用户控件.但很多时候看着很漂亮的东西你想进一步控制的时候却不那么如意.这里自己实现一次,功能不是高大全,但求一个清楚明白,也欢迎园友拍砖.前端是bootstrap3+jPaginate,后台基于membership.没什么难点. 先上效果图. 分页其实就是处理好 每页项目数.总项目数.总页数.当前页.为了方便复用,就先从仓库开始说起

ASP.NET MVC分页实现

ASP.NET MVC中不能使用分页控件,所以我就自己写了一个分页局部视图,配合PageInfo类,即可实现在任何页面任意位置呈现分页,由于采用的是基于POST分页方式,所以唯一的限制就是必须放在FORM中,当然以后我会考虑实现基于URL分页的! 一.PageInfo类 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 6 namespace ROIS.Mo

Asp.net MVC分页实例

分页是网页基本功能,这里主要讨论在Asp.net MVC环境下分页的前端实现,不涉及后台分页.实现效果如下图显示: Step 1.建立分页信息类 1 public class PagingInfo 2 { 3 public int TotalItmes { set; get; } 4 public int ItemsPerPage { set; get; } 5 public int CurrentPage { set; get; } 6 public int TotalPages 7 { 8

【随笔系列】Asp.Net Mvc分页控件PagedList的使用方法及配置

企业在做Asp.Net Mvc开发过程中,很多时候都是一些CRUD,最基本的就是一个列表页面,然后附带一些功能按钮.如果有数据列表,大多数就会涉及到对数据进行分页,这次就介绍一下Mvc PagedList控件分页的使用方法.Github PagedList链接 . 下面我通过新建Mvc项目来展示PagedList的使用方法. 一.新建BookLibrary解决方案 确定后,选择MVC 然后点击确定. 二.添加PagedList与PagedList.Mvc的程序包. 选择BookLibrary项目

基于Bootstrap的Asp.net Mvc 分页的实现

最近写了一个mvc 的 分页,样式是基于 bootstrap 的 ,提供查询条件,不过可以自己写样式根据个人的喜好,以此分享一下.首先新建一个Mvc 项目,既然是分页就需要一些数据,我这边是模拟了一些假的数据,实际的项目中都是在数据库中去取得的,很简单的数据: 1 public class User 2 { 3 public string Name { get; set; } 4 5 public int Age { get; set; } 6 } 取数据我这边是加了120个数据: 1 publ

ASP.NET MVC分页 Ajax+JsRender

前段时间整mvc的分页,倒是很顺利,参考了以下几篇博客,启发很大. http://www.cnblogs.com/tangmingjun/archive/2012/05/30/2526301.html http://www.cnblogs.com/tangmingjun/archive/2012/05/31/2528732.html 顺便扩展了需求,在分页的基础上,继续做关键字查询. 用PagedList生成的页码链接虽然样式很漂亮,但是要做到无刷新的分页,PagedList自动生成的代码是不够

基于Bootstrap的Asp.net Mvc 分页的实现(转)

最近写了一个mvc 的 分页,样式是基于 bootstrap 的 ,提供查询条件,不过可以自己写样式根据个人的喜好,以此分享一下.首先新建一个Mvc 项目,既然是分页就需要一些数据,我这 边是模拟了一些假的数据,实际的项目中都是在数据库中去取得的,很简单的数据: public class User { public string Name { get; set; } public int Age { get; set; } } 取数据我这边是加了120个数据: public List<User>