mvc HtmlHelper 分頁類 static的記得

來源:互聯網
上載者:User

標籤:class   blog   http   ext   string   os   

HtmlHelper的擴充:注意點:擴充方法必須是靜態方法,所在的類必須是靜態類,所在的命名空間改成System.Web.MVC則能省略頁面中必須添加命名空間的約束。//主要就是輸出分頁的超級連結的標籤//自訂分頁Helper擴充public static HtmlString ShowPageNavigate(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount)        {            var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath;            pageSize = pageSize == 0 ? 3 : pageSize;            var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //總頁數            var output = new StringBuilder();            if (totalPages > 1)            {                //if (currentPage != 1)                {//處理首頁串連                    output.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex=1&pageSize={1}‘>首頁</a> ", redirectTo, pageSize);                }                if (currentPage > 1)                {//處理上一頁的串連                    output.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>上一頁</a> ", redirectTo, currentPage - 1, pageSize);                }                else                {                   // output.Append("<span class=‘pageLink‘>上一頁</span>");                }                output.Append(" ");                int currint = 5;                for (int i = 0; i <= 10; i++)                {//一共最多顯示10個頁碼,前面5個,後面5個                    if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)                    {                        if (currint == i)                        {//當前頁處理                            //output.Append(string.Format("[{0}]", currentPage));                            output.AppendFormat("<a class=‘cpb‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>{3}</a> ", redirectTo, currentPage , pageSize, currentPage );                        }                        else                        {//一般頁處理                            output.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>{3}</a> ", redirectTo, currentPage + i - currint, pageSize, currentPage + i - currint);                        }                    }                    output.Append(" ");                }                if (currentPage < totalPages)                {//處理下一頁的連結                    output.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>下一頁</a> ", redirectTo, currentPage + 1, pageSize);                }                else                {                    //output.Append("<span class=‘pageLink‘>下一頁</span>");                }                output.Append(" ");                if (currentPage != totalPages)                {                    output.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>末頁</a> ", redirectTo, totalPages, pageSize);                }                output.Append(" ");            }            output.AppendFormat("第{0}頁 / 共{1}頁", currentPage, totalPages);//這個統計加不加都行            return new HtmlString(output.ToString());        }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.