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

时间:2022-10-12 16:34:04

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());
        }