MVC 下 使用MvcPager分页控件

时间:2022-08-03 04:11:57

先去下载 mvcpage http://mvcpager.codeplex.com/releases/view/64098只需要一个 MvcPager.dll文件即可 然后引用MvcPager.dll
如需了解更多 可以访问 http://www.webdiyer.com/Controls/MvcPager/Downloads

Models 引入命名空间

new { ID =ViewBag.ID } 这里放分页前的地址栏查询参数,没有可以不写

@using Webdiyer.WebControls.Mvc
@using MVC.Models
@using System.Data <div class="pages" style="text-align:left; margin-left:20px; margin-top:20px; width:80%" > <table>
<tr>
<td>@Html.Pager(Model, new PagerOptions { PageIndexParameterName = "currentPage", ShowPageIndexBox = true, PageIndexBoxType = PageIndexBoxType.DropDownList, ShowGoButton = false, PageIndexBoxWrapperFormatString = "跳转到{0}", FirstPageText = "首页",CssClass="pages" }, "default", new { ID =ViewBag.ID })
</td>
<td>共<span > @Model.TotalItemCount</span> 条记录,页 <span >@Model.CurrentPageIndex/@Model.TotalPageCount</span></td>
</tr>
</table> </div>

  

/*分页样式*/

.pages { color: #000000; }
.pages a { text-decoration:none; padding: 0 5px; border: 1px solid #ddd;background: #ffff;margin:0 2px; font-size:11px; color:#000;}
.pages a:hover { background-color: #E61636; color:#fff;border:1px solid #E61636; text-decoration:none;}
.pages span
{
color:red;
}

  

controllers

mvcpager 集成了  ToPagedList。使用之前必须使用排序 否则会报错,

private EFHelpDB db = new EFHelpDB();

public ActionResult Index(int id = 0, int currentPage = 1)
{ int pageSize = 10;//每页条数
ViewBag.ID = id;//分页之前的查询参数
return View(db.Me.Where(m => m.ParentID == id).OrderBy(m => m.Orderby).ToPagedList(currentPage, pageSize)); }