首页 > 代码库 > MVC 实现分页效果

MVC 实现分页效果

 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)            {                {//处理首页连接                    output.AppendFormat("<a class=‘pageLink‘ href=‘javascript:void(0)‘ onclick=‘pageShow(\"{0}\",1,{1});‘>首页</a>", redirectTo, pageSize);                }                if (currentPage > 1)                {                    //处理上一页的连接                    output.AppendFormat("<a class=‘pageLink‘ href=‘javascript:void(0)‘ onclick=‘pageShow(\"{0}\",{1},{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++)                {                    if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)                    {                        if (currint == i)                        {                            //当前页处理                            output.AppendFormat("<a class=‘on‘ href=‘javascript:void(0)‘ onclick=‘pageShow(\"{0}\",{1},{2});‘ id=‘oncurentPage‘>{3}</a>", redirectTo, currentPage, pageSize, currentPage);                        }                        else                        {                            //一般页处理                            output.AppendFormat("<a class=‘pageLink‘ href=‘javascript:void(0)‘ onclick=‘pageShow(\"{0}\",{1},{2});‘>{3}</a>", redirectTo, currentPage + i - currint, pageSize, currentPage + i - currint);                        }                    }                    output.Append(" ");                }                if (currentPage < totalPages)                {                    //处理下一页的连接                    output.AppendFormat("<a class=‘pageLink‘ href=‘javascript:void(0)‘ onclick=‘pageShow(\"{0}\",{1},{2});‘>下一页</a>", redirectTo, currentPage + 1, pageSize);                }                else                {                }                output.Append(" ");                if (currentPage != totalPages)                {                    output.AppendFormat("<a class=‘pageLink‘ href=‘javascript:void(0)‘ onclick=‘pageShow(\"{0}\",{1},{2});‘>末页</a>", redirectTo, totalPages, pageSize);                }                output.Append(" ");                output.AppendFormat("第{0}页 / 共{1}页", currentPage, totalPages);            }            return new HtmlString(output.ToString());        }
方法
@model System.Data.DataTable@{    Layout = null;}<div class="box-frame">    <h3>        待办工作</h3>    <table class="box-frame-table wordlist-table">        <thead>            <tr>                <th>                    主题                </th>                <th width="100">                    发件人                </th>                <th width="130">                    时间                </th>            </tr>        </thead>        <tbody>            @foreach (System.Data.DataRow item in Model.Rows)            {                 <tr tag="@item["Dia_Id"]">                    <td>@item["Pro_Title"]                    </td>                    <td>@item["C_aname"]                    </td>                    <td>@string.Format("{0:g}", item["Dia_StartTime"])                    </td>                </tr>            }        </tbody>    </table></div><p class="page_nav">    @Html.ShowPageNavigate(Convert.ToInt32(ViewData["pageIndex"]), Convert.ToInt32(ViewData["pageSize"]), Convert.ToInt32(ViewData["totalCount"]))</p><script type="text/javascript">    $(.wordlist-table tr).bind(click, function (event) {        window.location.href = "#test/todo";        analyseUrl("#test/todo");    });</script></body> </html> 
前台显示
        int pageIndex = 1;//当前页        int pageSize = 3;//每页显示条数  public ActionResult ToDoInfoList()        {            c_addressbook m_addressbook = Common.ReadCookie();            if (m_addressbook != null)            {                int.TryParse(Request.QueryString["pageIndex"], out pageIndex);                if (pageIndex == 0)                    pageIndex = 1;                string sqlCount = "select Dia_Id,pc_process.Pro_Title,pc_process.Pro_Sender,c_addressbook.C_aname,pc_dialog.Dia_StartTime from pc_dialog LEFT JOIN pc_process ON pc_dialog.Pro_Id =pc_process.Pro_id LEFT JOIN c_addressbook ON pc_process.Pro_Sender=c_addressbook.C_aid where pc_dialog.Dia_Manager=@manager and pc_dialog.Dia_State=-1";                string sql = "select Dia_Id,pc_process.Pro_Title,pc_process.Pro_Sender,c_addressbook.C_aname,pc_dialog.Dia_StartTime from pc_dialog LEFT JOIN pc_process ON pc_dialog.Pro_Id =pc_process.Pro_id LEFT JOIN c_addressbook ON pc_process.Pro_Sender=c_addressbook.C_aid where pc_dialog.Dia_Manager=@manager and pc_dialog.Dia_State=-1 order by Dia_StartTime desc LIMIT " + (pageIndex - 1) * pageSize + "," + pageSize + "";                MySqlParameter[] pars = new MySqlParameter[]{                new MySqlParameter("@manager",m_addressbook.C_aid)                };                int totalCount = PM.DBUtility.DbHelperMySQL.Query(sqlCount, pars).Tables[0].Rows.Count;                int pageCount = Convert.ToInt32(Math.Ceiling((double)totalCount / pageSize));//总页数                                if (pageIndex < 1)                    pageIndex = 1;                else if (pageIndex > pageCount)                    pageIndex = pageCount;                var newPage = PM.DBUtility.DbHelperMySQL.Query(sql, pars).Tables[0];                if (newPage.Rows.Count != 0)                {                    ViewData.Model = newPage;                    ViewData["pageCount"] = pageCount;                    ViewData["pageIndex"] = pageIndex;                    ViewData["pageSize"] = pageSize;                    ViewData["totalCount"] = totalCount;                }                ViewData.Model = newPage;            }            return View();        }
后台数据 使用方法

 

MVC 实现分页效果