Click here to Skip to main content
15,884,597 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This can run well with MSSQL,but after i change the database with mysql,it just show the firt
page, the viewdata.model can't get the next page record.

code in controllor:

index action:
C#
if (Request["page"] != null)
           {

               ViewData["page"] = SqlHelper.Pager(Request.RawUrl.Substring(0, Request.RawUrl.IndexOf("?")), Convert.ToInt32(Request["page"]), n.Count(), 5, 3);
               n = n.Skip((Convert.ToInt32(Request["page"]) - 1) * 5).Take(5);

           }
           else
           {
               ViewData["page"] = SqlHelper.Pager(Request.RawUrl, 1, n.Count(), 5, 3);

               n = n.Skip(0).Take(5);
           }



           return View(n);



code in SqlHelper:

XML
public static string Pager(string url1, int id, int count, int size, int show)
       {
           string pager = "";
           string url = url1.Contains("?") ? url1 + "&page=" : url1 + "?page=";
           pager = "<a href=\"" + url + (1) + "\"><<</a>";
           if (id != 1)
           {
               pager += "<a href=\"" + url + (id - 1) + "\"><</a>";
           }
           if (id > (show + 1))
           {
               pager += "<a href=\"" + url + (id - show - 1) + "\">..</a>";
           }

           int page_count = count / size;
           if (page_count == 0)
           {
               return "";
           }
           for (int i = id - show; i <= id + show; i++)
           {
               if (id == i)
                   pager += "<span>" + i + "</span>";
               else if (i > 0 && i <= page_count)
                   pager += "<a href=\"" + url + i + "\">" + i + "</a>";

           }
           if (id < page_count - show)
           {
               pager += "<a href=\"" + url + (id + show + 1) + "\">..</a>";

           }
           if (id != page_count)
           {
               pager += "<a href=\"" + url + (id + 1) + "\">></a>";
           }
           pager += "<a href=\"" + url + page_count + "\">" + page_count + "</a>";
           return pager;
       }


       public static string pagination(int total, int per, int page, string query_string)
       {
           int allpage = 0;
           int next = 0;
           int pre = 0;
           int startcount = 0;
           int endcount = 0;
           string pagestr = "";

           if (page < 1) { page = 1; }
           //计算总页数
           if (per != 0)
           {
               allpage = (total / per);
               allpage = ((total % per) != 0 ? allpage + 1 : allpage);
               allpage = (allpage == 0 ? 1 : allpage);
           }
           next = page + 1;
           pre = page - 1;
           startcount = (page + 5) > allpage ? allpage - 9 : page - 4;
           endcount = page < 5 ? 10 : page + 5;
           if (startcount < 1) { startcount = 1; } 
           if (allpage < endcount) { endcount = allpage; 
           pagestr = "totally" + allpage + "page&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

           pagestr += page > 1 ? "<a href=\"" + query_string + "?page=1\">first page</a>&nbsp;&nbsp;<a href=\"" + query_string + "?page=" + pre + "\">pre page</a>" : "first page next page";

           for (int i = startcount; i <= endcount; i++)
           {
               pagestr += page == i ? "&nbsp;&nbsp;<font color=\"#ff0000\">" + i + "</font>" : "&nbsp;&nbsp;<a href=\"" + query_string + "?page=" + i + "\">" + i + "</a>";
           }
           pagestr += page != allpage ? "&nbsp;&nbsp;<a href=\"" + query_string + "?page=" + next + "\">next page</a>&nbsp;&nbsp;<a href=\"" + query_string + "?page=" + allpage + "\">last page</a>" : " next page last page";

           return pagestr;
       }


index.aspx code:

XML
<% foreach (MvcBBS.Model.Article m in (IEnumerable)ViewData.Model)
       { %>
      <dt class="d"><%= Html.ActionLink(Html.Encode(m.Title), "Read", "Article", new { ID = m.ID}, null)%>&nbsp;&nbsp;&nbsp;&nbsp;
       <% if (Session["Flag"] == "1")
          { %>
         &nbsp;&nbsp;&nbsp;&nbsp; <span style="float:right">
          <%= Ajax.ActionLink("[Delete]", "Delete", new { ID = m.ID, SortID = m.Sort }, new AjaxOptions { UpdateTargetId = "divArticleList", Confirm="are you sure?" })%></span>
       <%} %>

       &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0099CC"><%= m.ReplyNum %></span>&nbsp;/&nbsp;<%= m.ReadNum %>
      &nbsp;&nbsp;&nbsp;&nbsp;<%= m.author%>
      &nbsp;&nbsp;&nbsp;&nbsp;<%= m.EntryDate %></dt>

    <%} %>
 <div class="page">&nbsp;&nbsp;<%=ViewData["page"] %>  </div>


related css:

CSS
.pagecomments{
    width: 500px;
    margin: 27px;
    padding: 27px;
}

.pagecomments h3{
    color: #6a6a6a;
}

.pagecomments a{
    color: #b7a795;
    text-decoration: none;
    font-size: 11px;
    background-color: #fdf8f1;
}

.pagecomments a:hover{
    color: #a9b880;
}

.pagecomments ol{
    list-style: none;
    margin: 10px 0;
    padding: 0;
}

.pagecomments ol li{
    list-style: none;
    margin: 10px 0 0;
    padding: 0 0 10px;
    border-bottom: 1px dotted #bbb7b0;
    line-height: 22px;
}

#pagecomments span.comment-author{
    font-weight: bold;
    text-decoration: underline;
}

#respond{
    padding: 10px;
    border: 1px dotted #bbb7b0;

}


someone would help me? thx a lot!!!
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900