页码条--字符串拼接--重写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,布布扣,bubuko.com

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

上一篇:值得记录的工具和网站


下一篇:js版俄罗斯方块(三)