码迷,mamicode.com
首页 > Web开发 > 详细

Asp.net MVC 使用PagedList 分页

时间:2015-11-07 00:48:38      阅读:328      评论:0      收藏:0      [点我收藏+]

标签:

在asp.net mvc 中,可以bootstrap来作为界面,自己来写分页程序。也可以使用PagedList来分页

1、首先,在NuGet程序包管理控制台 ,输入Install-Package PagedList.mvc 安装PagedList 和PageList.Mvc;查看程序中的引用,已经有了PagedList,和PagedList.Mvc两个文件的引用

2、使用,下面是一个实现了分页、排序、查找、分页功能的例子。通过多设置一个查找变量参数来保存当时的查找字符串 currentFilter,,分页、排序的时候都

使用currentFilter来作为参数传递。而一旦在 UI中重新输入查找字符串,就将页数设置为第一页。

控制器代码:

using PagedList;

public ActionResult Index(string sortOrder,string currentFilter,string searchString,int? page)
{
ViewBag.CurrentSort = sortOrder;

ViewBag.FirstNameSortParam = string.IsNullOrEmpty(sortOrder) ? "FirstName_desc" : "";
ViewBag.LastNameSortParam = sortOrder == "LastName" ? "LastName_desc" : "LastName";
ViewBag.DateSortParam = sortOrder == "date" ? "date_desc" : "date";

var students = from s in db.Students
select s;

if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}

ViewBag.CurrentFilter = searchString;

if (!string.IsNullOrEmpty(searchString))
{
students= students.Where(s => s.LastName.ToUpper() .Contains(searchString.ToUpper())
|| s.FirstMidName.ToUpper().Contains(searchString.ToUpper()));
}
//ViewBag.searchString = searchString;

switch (sortOrder)
{
case "FirstName_desc":
students = students.OrderByDescending(s => s.FirstMidName);
break;
case "LastName":
students = students.OrderBy(s => s.LastName);
break;
case "LastName_desc":
students = students.OrderByDescending(s => s.LastName);
break;
case "date":
students =students.OrderBy(s =>s.EnrollmentDate);
break;
case "date_desc":
students = students.OrderByDescending(s => s.EnrollmentDate);
break;
default:
students = students.OrderBy(s => s.FirstMidName);
break;
}

int pageSize = 2;
int pageNumber = (page ?? 1);

return View(students.ToPagedList(pageNumber,pageSize));
}

视图代码:

@model PagedList.IPagedList<ContosoUniversity.Models.Student>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
@{
ViewBag.Title = "学生";
}

<h2>@ViewBag.Title</h2>

<p>
@Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm("Index", "Student", FormMethod.Get, new { @class="form-inline",role="form" }))
{
<label for="searchString" class="control-label">通过名字查找:</label>
<div class="form-group">
@Html.TextBox("searchString", ViewBag.CurrentFilter as string, htmlAttributes: new { @class = "form-control", placeholder = "请输入名字" })   //只是在文本框中回显当时查找的字符串,并没 将 name为searchString的文本框表单提交
</div>

<input type="submit" value="查找" class="btn btn-primary" />
}
<table class="table table-hover table-striped">
<thead>
<tr>
<th>
@Html.ActionLink("名", "Index", new { sortOrder = ViewBag.LastNameSortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("姓", "Index", new { sortOrder = ViewBag.FirstNameSortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("入学时间", "Index", new { sortOrder = ViewBag.DateSortParam, currentFilter = ViewBag.CurrentFilter })
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstMidName)
</td>
<td>
@Html.DisplayFor(modelItem => item.EnrollmentDate)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
@Html.ActionLink("Details", "Details", new { id = item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ID })
</td>
</tr>
}
</tbody>
</table>
<br />
Page @(Model.PageCount< Model.PageNumber ? 0: Model.PageNumber)of @Model.PageCount

@Html.PagedListPager(Model, page => Url.Action("Index",
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))

Asp.net MVC 使用PagedList 分页

标签:

原文地址:http://www.cnblogs.com/liuyuanhao/p/4943940.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!