MVC 如何分页,还有点击下一页的时候能传递查询FORM里的参数

解决方案 »

  1.   

    这个是我做分页的时候写的,因为有筛选条件,所以用POST提交的,然后是把当前页保存到了SESSION中/// <summary>
            /// 分页Pager显示
            /// </summary>
            /// <param name="html"></param>
            /// <param name="currentPageStr">标识当前页码的QueryStringKey</param>
            /// <param name="pageSize">每页显示</param>
            /// <param name="totalCount">总数据量</param>
            /// <returns></returns>
            public static MvcHtmlString Pager(this HtmlHelper html, int pageSize, int totalCount)
            {
                int currentPage;
                int.TryParse(HttpContext.Current.Request.Form["currentPageIndex"], out currentPage); //与相应的QueryString绑定
                if (currentPage == 0)
                    if (CurrentP != "" && CurrentP != null)
                        currentPage = int.Parse(CurrentP);
                    else
                        currentPage = 1;
                var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
                var output = new StringBuilder();
                output.AppendLine("<input type=\"hidden\" id=\"currentPageIndex\" name=\"currentPageIndex\" value=\"" + (HttpContext.Current.Request.Form["currentPageIndex"] == null ? currentPage.ToString() : HttpContext.Current.Request.Form["currentPageIndex"]) + "\"/>");            if (totalPages > 1)
                {
                    if (currentPage != 1)
                    {//处理首页连接
                        output.AppendFormat("{0} ", html.RouteLink("首页", new { }, new { onclick = "return Goto(" + 1 + ")" }));
                    }
                    if (currentPage > 1)
                    {//处理上一页的连接 
                        output.Append(html.RouteLink("上一页", new { }, new { onclick = "return Prev_Goto(" + (currentPage - 1) + ")" }));
                    }
                    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
                            {//一般页处理  
                                output.Append(html.RouteLink((currentPage + i - currint).ToString(), new { }, new { onclick = "return Goto(" + (currentPage + i - currint) + ")" }));
                            }
                        output.Append(" ");
                    }
                    if (currentPage < totalPages)
                    {//处理下一页的链接 
                        output.Append(html.RouteLink("下一页", new { }, new { onclick = "return Next_Goto(" + (currentPage + 1) + ")" }));
                    }
                    else
                    {
                        output.Append("下一页");
                    }
                    output.Append(" ");
                    if (currentPage != totalPages)
                    {
                        output.Append(html.RouteLink("末页", new { }, new { onclick = "return Goto(" + totalPages + ")" }));
                    }
                    output.Append(" ");
                }
                output.AppendFormat("{0} / {1}", currentPage, totalPages);//这个统计加不加都行             output.AppendLine("");
                output.AppendLine("<script type=\"text/javascript\">");
                output.AppendLine("function Goto(p) {");
                output.AppendLine("$(\"#currentPageIndex\").val(p);");
                output.AppendLine("$(\"form\").attr(\"action\", \"Index?p=\" + p);");
                output.AppendLine("$(\"form\").submit();");
                output.AppendLine("return false;");
                output.AppendLine("}");
                output.AppendLine("function Prev_Goto(p) {");
                output.AppendLine("$(\"#currentPageIndex\").val(parseInt($(\"#currentPageIndex\").val()) - 1);");
                output.AppendLine("$(\"form\").attr(\"action\", \"Index?p=\" + p);");
                output.AppendLine("$(\"form\").submit();");
                output.AppendLine("return false;");
                output.AppendLine("}");
                output.AppendLine("function Next_Goto(p) {");
                output.AppendLine("$(\"#currentPageIndex\").val(parseInt($(\"#currentPageIndex\").val()) + 1);");
                output.AppendLine("$(\"form\").attr(\"action\", \"Index?p=\" + p);");
                output.AppendLine("$(\"form\").submit();");
                output.AppendLine("return false;");
                output.AppendLine("}");
                output.AppendLine("</script>");            return new MvcHtmlString(output.ToString());
            }
      

  2.   


    public static string CurrentP
            {
                get
                {
                    return HttpContext.Current.Session["currentPage"] == null ? "" : HttpContext.Current.Session["currentPage"].ToString();
                }
            }