Part 63 - mvc 分页

By the end of this section, the index page should support both search functionality andpagination as shown below.  

Implement paging in asp.net mvc application 

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. 
PagedList nuget package 

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 })  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值