Part 63 - mvc 分页
来源:互联网 发布:linux 源码 mysql 编辑:程序博客网 时间:2024/05/29 07:44
Step 1: Install PagedList.Mvc using NuGet package manager. PagedList.Mvc is dependent on PagedList. Installing PagedList.Mvc will automatically install PagedList package as well.
Step 2: Include the following using statements in HomeController.cs file
using PagedList.Mvc;
using PagedList;
Modify the Index() action method as shown below. Notice that we are passing pageparameter to this function. This parameter is used for specifying the page number. This parameter can be null, and that's the reason we have chosen a nullable integer. We convert the list, to a paged list, using ToPagedList(). Also, notice that, we are using null-coalescing operator. If the "page" parameter is null, then 1 is passed as the page number, else, the value contained in the "page" parameter is used as the page number.
public ActionResult Index(string searchBy, string search, int? page)
{
if (searchBy == "Gender")
{
return View(db.Employees.Where(x => x.Gender == search || search ==null).ToList().ToPagedList(page ?? 1, 3));
}
else
{
return View(db.Employees.Where(x => x.Name.StartsWith(search) || search ==null).ToList().ToPagedList(page ?? 1, 3));
}
}
Step 3: Make the following modifications to Index.cshtml view
a) Include the following 2 using statements on the view.
@using PagedList.Mvc;
@using PagedList;
b) The model for the view should be IPagedList<Employee>.
@model IPagedList<MVCDemo.Models.Employee>
c) Since, we have changed the model of the view, fromIEnumerable<MVCDemo.Models.Employee> toIPagedList<MVCDemo.Models.Employee>, change the section that displays table headings as shown below.
<tr>
<th>
@Html.DisplayNameFor(model => model.First().Name)
</th>
<th>
@Html.DisplayNameFor(model => model.First().Gender)
</th>
<th>
@Html.DisplayNameFor(model => model.First().Email)
</th>
<th>Action</th>
</tr>
d) Finally to display page numbers for paging
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, searchBy = Request.QueryString["searchBy"], search = Request.QueryString["search"] }))
e) If you want to display the pager, only if there are more than 1 page
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, searchBy = Request.QueryString["searchBy"], search = Request.QueryString["search"] }), newPagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded })
f) If you want to display, the current active page and the total number of pages
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, searchBy = Request.QueryString["searchBy"], search = Request.QueryString["search"] }), newPagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded, DisplayPageCountAndCurrentLocation = true })
g) If you want to display the number of rows displayed, of the total number of rows available.
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, searchBy = Request.QueryString["searchBy"], search = Request.QueryString["search"] }), newPagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded, DisplayItemSliceAndTotal = true })
- Part 63 - mvc 分页
- MVC 分页
- MVC分页
- MVC分页
- MVC分页
- MVC分页
- mvc分页
- MVC 分页
- MVC - Part 01
- Part 64 - MVC Sorting 排序
- 分页控件-Jquery-MVC
- MVC MvcPager Ajax分页
- MVC MvcPager 普通分页
- spring mvc 分页
- mvc中分页
- MVC 自动分页控件
- MVC 3.0分页
- 【MVC分页】页码
- HDU 2089 不要62 (数位dp入门题)
- 全选,反选,全不选源代码
- Could not open Hibernate Session for transaction 解决方案
- 第八周拓展实践(3)换分币
- node js 进阶-node的多核实现
- Part 63 - mvc 分页
- 地图坐标转换 -- 火星坐标与GPS坐标
- android 使用shape 画虚线
- 排序
- 计算手续费
- Java学习之InputStream中read()与read(byte[] b)
- Spring IOC设计原理解析:本文乃学习整理参考而来
- 同步工具类--闭锁、信号量、栅栏的总结
- 关于利用JDBC连接sqlserver中数据库的问题