1:首先需要下载MvcPager 2.0以上版本即可
2:新建分布视图,添加webdiyer引用于强类型引用
要注意的是ajax.pager里面的DIV的id要与主视图中的id一致,刷新是整个div刷新的
1 @using Webdiyer.WebControls.Mvc 2 @model IPagedList<MvcApplication1.Models.Student> 3 <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script> 4 <table> 5 <tr> 6 <th>序号</th> 7 <th>年龄</th> 8 <th>姓名</th> 9 </tr> 10 @foreach (var item in Model) 11 { 12 <tr> 13 <td>@item.DocId</td> 14 <td>@item.StuAge</td> 15 <td>@item.StuName</td> 16 </tr> 17 } 18 </table> 19 @Ajax.Pager(Model, new PagerOptions { PageIndexParameterName = "id", ShowPageIndexBox = true, PageIndexBoxType = PageIndexBoxType.DropDownList, ShowGoButton = false }, new MvcAjaxOptions { UpdateTargetId = "divList", OnBegin = "$('#divList').fadeOut('slow')", OnComplete = "$('#divList').fadeIn('slow')" }) 20 @*@section Scripts{@{Html.RegisterMvcPagerScriptResource();}}*@ 21 <script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
3:添加主视图,需要注意的是 js一定要引用,无刷新就是靠js传递完成的
1 @using Webdiyer.WebControls.Mvc 2 @model Webdiyer.WebControls.Mvc.PagedList<MvcApplication1.Models.Student> 3 @{ 4 ViewBag.Title = "Index"; 5 } 6 @*<script src="~/Scripts/jquery-1.8.2.js"></script>*@ 7 <script src="~/Scripts/jquery.validate.unobtrusive.js"></script> 8 <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script> 9 <div class="container"> 10 <div id="divList"> 11 @Html.Partial("StudentList", Model); 12 </div> 13 @section Scripts{@{Html.RegisterMvcPagerScriptResource();}} 14 </div>
4:controller 代码,需要注意的是 返回的model一定得是IPagedList<T>类型的
1 public ActionResult Index(int? id, string name) 2 { 3 id = id ?? 1; 4 int pageSize = 3; 5 MyTestEntities db = new MyTestEntities(); 6 IQueryable<Student> temp = db.Student.Where(c => true).OrderBy(c => c.DocId); 7 IPagedList<Student> pageList = temp.AsQueryable<Student>().ToPagedList<Student>(id.Value, pageSize); 8 if (Request.IsAjaxRequest()) 9 { 10 return PartialView("StudentList", pageList); 11 } 12 return View(pageList);
效果图: