我用了网上的分页控件:public static string Pager(this HtmlHelper html, string currentPageStr, int pageSize, int totalCount)
        {
            var queryString = html.ViewContext.HttpContext.Request.QueryString;
            int currentPage = 1; //当前页  
            var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数  
            var dict = new System.Web.Routing.RouteValueDictionary(html.ViewContext.RouteData.Values);
            var output = new System.Text.StringBuilder();
            if (!string.IsNullOrEmpty(queryString[currentPageStr]))
            {
                //与相应的QueryString绑定 
                foreach (string key in queryString.Keys)
                    if (queryString[key] != null && !string.IsNullOrEmpty(key))
                        dict[key] = queryString[key];
                int.TryParse(queryString[currentPageStr], out currentPage);
            }
            else
            {
                //获取 ~/Page/{page number} 的页号参数
                if (dict.ContainsKey(currentPageStr))
                    int.TryParse(dict[currentPageStr].ToString(), out currentPage);
            }
            if (currentPage <= 0) currentPage = 1;
            if (totalPages > 1)
            {
                if (currentPage != 1)
                {
                    //处理首页连接  
                    dict[currentPageStr] = 1;
                    output.AppendFormat("{0} ", html.RouteLink("首页", dict));
                }
                if (currentPage > 1)
                {
                    //处理上一页的连接  
                    dict[currentPageStr] = currentPage - 1;
                    output.Append(html.RouteLink("上一页", dict));
                }
                else
                {
                    output.Append("上一页");
                }
                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));
                        }
                        else
                        {
                            //一般页处理 
                            dict[currentPageStr] = currentPage + i - currint;
                            output.Append(html.RouteLink((currentPage + i - currint).ToString(), dict));
                        }
                    output.Append(" ");
                }
                if (currentPage < totalPages)
                {
                    //处理下一页的链接 
                    dict[currentPageStr] = currentPage + 1;
                    output.Append(html.RouteLink("下一页", dict));
                }
                else
                {
                    output.Append("下一页");
                }
                output.Append(" ");
                if (currentPage != totalPages)
                {
                    dict[currentPageStr] = totalPages;
                    output.Append(html.RouteLink("末页", dict));
                }
                output.Append(" ");
            }
            output.AppendFormat("{0} / {1}", currentPage, totalPages);//这个统计加不加都行 
            return output.ToString();
        }
问题:
数据可以成功显示,但是页面上的分页功能无法正常实现
页面开始显示第一页是正常的,点击第二页数据就跳到了第三页,点击第四页就跳到第五页,以此类推
返回来点击第一页却显示的是第二页,第一页就是显示不出来谁能帮我看看代码那里有错误,怎么改才能正确的显示出来,谢谢,问题解决马上给分代码引用:http://www.cnblogs.com/chsword/archive/2009/05/10/mvcskill_2.html

解决方案 »

  1.   

    ASP.NET MVC分页控件MvcPager http://topic.csdn.net/u/20100614/16/74e6f38d-7b7a-4679-a9df-9ebb5c662230.html
      

  2.   

    看我的博客,分页的   www.suncl.net
      

  3.   

    namespace MyPager
    {
        public class PageBar
        {
            public PageBar(int totalRec, int currentPage, int pageSize)
            {
                this.totalRec = totalRec;
                this.currentPage = currentPage;
                this.pageSize = pageSize;
                init();
            }
            private void init(){
                this.firstPage = 1;
                this.totalPage = this.totalRec % this.pageSize == 0 ? this.totalRec / this.pageSize : this.totalRec / this.pageSize + 1;
                this.lastPage = this.totalPage;
                this.nextPage = this.currentPage < this.totalPage ? this.currentPage + 1 : this.currentPage;
                this.prePage = this.currentPage > 1 ? this.currentPage - 1 : this.currentPage;
            }
            private int firstPage;         public int FirstPage
            {
                get { return firstPage; }
                set { firstPage = value; }
            }
            private int lastPage;        public int LastPage
            {
                get { return lastPage; }
                set { lastPage = value; }
            }        private int nextPage;        public int NextPage
            {
                get { return nextPage; }
                set { nextPage = value; }
            }
            private int prePage;        public int PrePage
            {
                get { return prePage; }
                set { prePage = value; }
            }
            private int totalPage;        public int TotalPage
            {
                get { return totalPage; }
                set { totalPage = value; }
            }
            private int currentPage;        public int CurrentPage
            {
                get { return currentPage; }
                set { currentPage = value; }
            }
            private int pageSize;        public int PageSize
            {
                get { return pageSize; }
                set { pageSize = value; }
            }
            private int totalRec;        public int TotalRec
            {
                get { return totalRec; }
                set { totalRec = value; }
            }
            private string buildFirst() {
                StringBuilder sb = new StringBuilder();
                if (this.currentPage == 1)
                {
                    sb.Append("首页");
                }
                else {
                    sb.Append("<a href='?page="+this.firstPage+"' id='first'>首页</a>");
                }
                return sb.ToString();
            }
            private string buildpre() {
                StringBuilder sb = new StringBuilder();
                if (this.currentPage > 1)
                {
                    sb.Append("<a href='?page=" + this.prePage + "' id='first'>上一页</a>");
                }
                else {
                    sb.Append("上一页");
                }
                return sb.ToString();
            }
            private string buildnext()
            {
                StringBuilder sb = new StringBuilder();
                if (this.currentPage < this.totalPage)
                {
                    sb.Append("<a href='?page=" + this.nextPage + "' id='first'>下一页</a>");
                }
                else
                {
                    sb.Append("下一页");
                }
                return sb.ToString();
            }
            private string buildLast()
            {
                StringBuilder sb = new StringBuilder();
                if (this.currentPage == this.totalPage)
                {
                    sb.Append("末页");
                }
                else
                {
                    sb.Append("<a href='?page=" + this.lastPage + "' id='first'>末页</a>");
                }
                return sb.ToString();
            }
            public string footer() {
                StringBuilder sb = new StringBuilder();
                sb.Append("<div style='font-size:12px;'>");
                sb.Append("总记录:"+this.totalRec+"  每页"+this.pageSize+"条  共"+this.totalPage+"页  "+buildFirst()+" "+this.buildpre()+" 当前 "+this.currentPage+" 页 "+this.buildnext()+" "+this.buildLast()+" 转到 <input name='p' id='p' type='text' value='"+this.currentPage+"' size='2' /> 页 <input type='button' name='button' onclick=\"jump(document.getElementById('p').value);\" value='确定' />");
                sb.Append("</div>");
                sb.Append("<script>function jump(obj){window.location=\"?page=\" + obj + \"\"}</script>");
                return sb.ToString();
            }
        }
    }           
      

  4.   

    调用代码:
      public ActionResult Index()
            {
                ServiceFacade sf = new ServiceFacade();
                UserService userService = sf.getUserService();
                int totalRec = userService.totalUser() ;
                int currentPage = String.IsNullOrEmpty(Request.QueryString["page"])?1:Convert.ToInt32(Request.QueryString["page"]);
                int pageSize =20;
                PageBar pb = new PageBar(totalRec, currentPage, pageSize);
                IList<User> ulist = userService.userList((pb.CurrentPage-1)*pb.PageSize, pb.PageSize);            ViewData["ReturnList"] = ulist;
                ViewData["footer"] = pb.footer();            return View();
            }
      

  5.   

    点击第二页数据就跳到了第三页  
    说明第二页的url参数不对,计算参数出错。如page=2 却成了page=3
    仔细调试一下。
      

  6.   

    是不是从第0页开始算起啊。if (currentPage  <= 0) currentPage = 1; 当你点第二页的时候,那不就是第三页拉。推测。自己仔细跟踪下。
      

  7.   

    1、for循环绑定的时候是从0开始,lz可能把跟i加减的时候成为1了
    2、计算当前页的时候+1
    反正就是+1的问题,lz试试....