日期:2014-05-17  浏览次数:20587 次

ASP.NET MVC 分页问题

/// <summary>
        /// 分页
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="pageIndex">当前页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="DataCount">数据总数</param>
        /// <param name="htmlAttributes">HTML特性</param>
        /// <returns>返回分页HTML字符</returns>
        public static HtmlString PageTurning(this HtmlHelper helper,int pageIndex,int pageSize, int DataCount,object htmlAttributes)
        {
            
            int pageCount = DataCount % pageSize == 0 ? DataCount / pageSize : DataCount / pageSize+1;
            RouteData routeData = helper.ViewContext.RouteData;
            string action = routeData.Values["Action"].ToString();
            string controller = routeData.Values["Controller"].ToString().ToLower();
            string url = controller + "/" + action;
            var redirectTo = helper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath;  
            StringBuilder html = new StringBuilder();
            html.Append("<div>");
            html.AppendFormat("<a href='{0}?pageIndex={1}'>首页</a>", url, 1);
            if (pageCount > 1)
            {
                if (pageIndex <=1)
                {
                    html.Append("<a herf=javascript:void(0)>上一页</a>");
                }
                else
                {
                    html.AppendFormat("<a href='{0}?pageIndex={1}'>『上一页』</a>", url, pageIndex - 1);
          &nbs