分页列表的示例-ASP.NETMVC
本文关键字:-ASP NETMVC 列表 分页 | 更新日期: 2023-09-27 18:29:48
对包含列表的模型进行分页的最佳方式是什么?
根据下面的模型,我希望浏览我的公司模型上的"电子邮件"。我目前正在尝试使用PagedListMVC来实现这一点。
型号示例
public class EmailModel
{
public Guid Id { get; set; }
public string EmailMessage { get; set; }
public Guid CompanyId { get; set; }
}
public class CompanyModel
{
public Guid Id { get; set; }
public string CompanyName { get; set; }
public IPagedList<Email> Emails { get; set; }
}
我的页面有一个显示电子邮件的部分视图
@Html.Partial("_CompanyEmails.cshtml", Model.Emails);
有人能给我举一个控制器的例子来处理这个问题吗?
您查看过文档吗?
页面下方大约三分之二的标题是:
为索引方法添加分页功能
控制器
给出的样本(在其上下文中)是:
using PagedList;
...
...
public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.CurrentFilter = searchString;
var students = from s in db.Students
select s;
if (!String.IsNullOrEmpty(searchString))
{
students = students.Where(s => s.LastName.ToUpper().Contains(searchString.ToUpper())
|| s.FirstMidName.ToUpper().Contains(searchString.ToUpper()));
}
switch (sortOrder)
{
case "name_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: // Name ascending
students = students.OrderBy(s => s.LastName);
break;
}
int pageSize = 3;
int pageNumber = (page ?? 1);
return View(students.ToPagedList(pageNumber, pageSize));
}
查看
如果没有要显示的页面;第0页,共0页";显示。(在这种情况下,页码大于页数,因为Model.PageNumber为1,Model.PageCount为0。)
页面按钮由PagedListPager助手显示:
@Html.PagedListPager( Model, page => Url.Action("Index", new { page }) )