页码条--字符串拼接--重写HtmlHelper

  public static HtmlString ShowPageNavigate(this HtmlHelper htmlHelper, int currentPage, int pageSize, int pageCount)
        {
            //处理Url保留除pageIndex和pageSize以外的其他参数
            var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath;
            redirectTo += "?";
            string strPageIndex = "pageIndex";
            string strPageSize = "pageSize";
            foreach (string k in htmlHelper.ViewContext.RequestContext.HttpContext.Request.QueryString.Keys)
            {
                if (string.Compare(k, strPageIndex, true) != 0 && string.Compare(k, strPageSize, true) != 0)
                {
                    redirectTo += k + "=" + htmlHelper.ViewContext.RequestContext.HttpContext.Request.QueryString[k] + "&";
                }
            }
            redirectTo = redirectTo.TrimEnd(new char[] { ‘&‘ });

            pageSize = pageSize == 0 ? 3 : pageSize;
            // var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
            var totalPages = pageCount;
            var output = new StringBuilder();
            if (totalPages > 1)
            {
                if (currentPage > 1)
                {
                    output.AppendFormat("<li ><span><a class=‘pageLink‘ href=‘{0}&pageIndex={1}&pageSize={2}‘>上一页</a></span></li> ", redirectTo, currentPage - 1, pageSize);
                }
                if (currentPage != 1)
                {
                    output.AppendFormat("<li ><span><a class=‘pageLink‘ href=‘{0}&pageIndex={1}&pageSize={2}‘>1</a></span></li> ", redirectTo, 1, pageSize);
                }
                if (currentPage > 3)
                {
                    output.AppendFormat("<li ><span><a class=‘pageLink‘>...</a></span></li> ");
                }
                if (currentPage > 2)
                {
                    output.AppendFormat("<li ><span><a class=‘pageLink‘ href=‘{0}&pageIndex={1}&pageSize={2}‘>{3}</a></span></li> ", redirectTo, currentPage - 1, pageSize, currentPage - 1);
                }
                //当前页
                if (currentPage > 0 && currentPage <= totalPages)
                {
                    output.AppendFormat("<li class=‘active‘><span><a class=‘pageLink‘ href=‘{0}&pageIndex={1}&pageSize={2}‘>{3}</a></span></li> ", redirectTo, currentPage, pageSize, currentPage);
                }
                if (currentPage + 1 < totalPages)
                {
                    output.AppendFormat("<li ><span><a class=‘pageLink‘ href=‘{0}&pageIndex={1}&pageSize={2}‘>{3}</a></span></li> ", redirectTo, currentPage + 1, pageSize, currentPage + 1);

                }
                if (currentPage + 2 < totalPages)
                {
                    output.AppendFormat("<li ><span><a class=‘pageLink‘>...</a></span></li> ");

                }
                if (currentPage != totalPages)
                {
                    output.AppendFormat("<li ><span><a class=‘pageLink‘ href=‘{0}&pageIndex={1}&pageSize={2}‘>{3}</a></span></li> ", redirectTo, totalPages, pageSize, totalPages);
                }
                if (currentPage < totalPages)
                {
                    output.AppendFormat("<li ><span><a class=‘‘  href=‘{0}&pageIndex={1}&pageSize={2}‘>下一页</a></span></li> ", redirectTo, currentPage + 1, pageSize);
                }
            }
            return new HtmlString(output.ToString());
        }

页码条--字符串拼接--重写HtmlHelper,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。