【MVC分页】页码

注:自己的代码收藏,对各位或许没有用。
1) 拓展 HtmlHelper 类
public static class HtmlHelperEx
{
    private const string AnchorFormat = "<a class=\"GridPager_Page\" href=\"?{0}&pageIndex={1}\">{2}</a>";
    private const string PlaceHolderFormat = "<span class=\"GridPager_Page\">{0}</span>";
    private const string HiddenPage = "...";
    private const string BeginDiv = "<div class=\"GridPager\">";
    private const string EndDiv = "</div>";
    private const string PrevPageText = "<<Prev";
    private const string NextPageText = "Next>>";

    private const int MAX_PAGE_DISPLAY = 10;// 1,2,3,4,5,6,7,8,9,10
    private const int PAGE_COUNT_NEWXT_TO_CURRENT_PAGE = 2;// 1,2 ..(5,6),[7],(8,9)..20,10
    private const int PAGE_COUNT_AT_END = 2;// (1,2) ..5,6,[7],8,9..(20,21)

    public static MvcHtmlString GridPager(this HtmlHelper htmlHelper)
        {
            StringBuilder html = new StringBuilder(BeginDiv);

            string queryStr = htmlHelper.ViewBag.QueryString;

            int pageIndex = htmlHelper.ViewBag.PageIndex;
            int pageCount = htmlHelper.ViewBag.PageCount;

            int nextPage = pageIndex;
            if (nextPage < 1) { nextPage = 1; }
            if (nextPage > pageCount) { nextPage = pageCount; }

            if (nextPage > 1)
            {
                html.AppendFormat(AnchorFormat, queryStr, nextPage - 1, PrevPageText);
            }

            if (pageCount > MAX_PAGE_DISPLAY)
            {
                AddAnchor(html, nextPage, 1, PAGE_COUNT_AT_END, queryStr);

                if (nextPage - PAGE_COUNT_NEWXT_TO_CURRENT_PAGE > PAGE_COUNT_AT_END + 2)
                {
                    // 1,2 ... 5,6,[7]
                    int beginPage = nextPage - PAGE_COUNT_NEWXT_TO_CURRENT_PAGE;
                    if (nextPage + PAGE_COUNT_NEWXT_TO_CURRENT_PAGE * 2 >= pageCount)
                    {
                        // 1,2 .. 13,14,15,16,17,18,19,20
                        beginPage = pageCount - (MAX_PAGE_DISPLAY - PAGE_COUNT_AT_END) + 1;
                    }
                    if (beginPage <= PAGE_COUNT_AT_END)
                    {
                        beginPage = PAGE_COUNT_AT_END + 1;
                    }
                    html.AppendFormat(PlaceHolderFormat, HiddenPage);
                    AddAnchor(html, nextPage, beginPage, nextPage, queryStr);
                }
                else
                {
                    // 1,2,3,4,5,[6]
                    AddAnchor(html, nextPage, PAGE_COUNT_AT_END + 1, nextPage, queryStr);
                }

                if (pageCount - PAGE_COUNT_AT_END > nextPage + PAGE_COUNT_NEWXT_TO_CURRENT_PAGE + 1)
                {
                    // [6],7,8 .. 11,12
                    int tillPage = nextPage + PAGE_COUNT_NEWXT_TO_CURRENT_PAGE;
                    if (tillPage < MAX_PAGE_DISPLAY - PAGE_COUNT_AT_END)
                    {
                        // 1,2,3,4,5,6,7,8 .. 19,20
                        tillPage = MAX_PAGE_DISPLAY - PAGE_COUNT_AT_END;
                    }
                    AddAnchor(html, nextPage, nextPage == 1 ? PAGE_COUNT_AT_END + 1 : nextPage + 1, tillPage, queryStr);
                    html.AppendFormat(PlaceHolderFormat, HiddenPage);
                    AddAnchor(html, nextPage, pageCount - 1, pageCount, queryStr);
                }
                else
                {
                    // [6],7,8,9,10,11
                    AddAnchor(html, nextPage, nextPage + 1, pageCount, queryStr);
                }
            }
            else
            {
                if (pageCount > 1)
                {
                    AddAnchor(html, nextPage, 1, pageCount, queryStr);
                }
            }

            if (pageCount - nextPage > 0)
            {
                html.AppendFormat(AnchorFormat, queryStr, nextPage + 1, NextPageText);
            }

            return new MvcHtmlString(html.Append(EndDiv).ToString());
        }

    private static void AddAnchor(StringBuilder html, int current, int from, int to, string queryStr)
        {
            for (int i = from; i <= to; i++)
            {
                if (current == i)
                {
                    html.AppendFormat(PlaceHolderFormat, i);
                    continue;
                }
                html.AppendFormat(AnchorFormat, queryStr, i, i);
            }
        }
}
2) 定义自己的ControllerBase 来辅助完成一些功能
public class GridControllerBase : DataControllerBase
{
    private int pageIndex = 1;
    private int pageCount = 0;

    protected char DateDelimiter = '~';

    /// <summary>
        /// Current page index(begin with 1)
        /// </summary>
    protected int PageIndex
        {
            get 
            {
                // the final adjustment.
                if (this.pageIndex > this.pageCount)
                {
                    return this.pageCount;
                }

                return pageIndex; 
            }
        }

    /// <summary>
        /// The total page count.
        /// </summary>
    protected int PageCount
        {
            get { return pageCount; }
            set { pageCount = value; }
        }

    /// <summary>
        /// Page Size.
        /// </summary>
    protected int PageSize
        {
            get { return 10; }
        }

    public string SearchKey { get; set; }

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

            int.TryParse(Request.QueryString["pageIndex"] ?? "1", out this.pageIndex);

            if (this.pageIndex < 1)
            {
                this.pageIndex = 1;
            }

            this.SearchKey = Request.QueryString["searchKey"];
        }

    protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);

            ViewBag.PageIndex = this.pageIndex > this.pageCount ? this.pageCount : this.pageIndex;
            ViewBag.PageCount = this.pageCount;
            ViewBag.QueryString = "searchKey=" + this.SearchKey;
            ViewBag.SearchKey = this.SearchKey;
        }

    /// <summary>
        /// Evaluate page count.
        /// </summary>
        /// <param name="totalCount">Total record count.</param>
        /// <returns></returns>
    protected int GetPageCount(int totalCount)
        {
            return (totalCount + this.PageSize - 1) / this.PageSize;
        }
}
3) 用法
@if (Model == null || Model.Count() == 0)
{ 
    <div>
        No data, please refine your search.
    </div>
}
else
{
    @Html.GridPager()
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值