分页列表示例 - ASP .NET MVC

时间:2014-11-10 14:00:54

标签: c# asp.net-mvc razor

寻找包含列表的模型的最佳方法是什么?

根据以下型号,我希望在我的公司模型上翻阅“电子邮件”。我目前正在尝试使用PagedList MVC来实现这一目标。

示例模型

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

有人能告诉我一个控制器来处理这个问题吗?

1 个答案:

答案 0 :(得分:0)

您是否查看了docs

页面下方约三分之二是标题:

  

将分页功能添加到索引方法


控制器


给出的样本(在其上下文中)是:

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

查看


如果没有要显示的页面,&#34; Page 0 of 0&#34;显示。 (在这种情况下,页码大于页数,因为Model.PageNumber为1,而Model.PageCount为0。)

分页按钮由PagedListPager助手显示:

@Html.PagedListPager( Model, page => Url.Action("Index", new { page }) )