我使用List而不是IEnumerable模型
我的控制器
public ActionResult Index(int? page)
{
var pageNumber = page ?? 1;
var itemCount = employees.ToPagedList(pageNumber, 5);
return View(employees.ToList());
}
我的观点
@Html.Partial("EmployeeList", Model.AsEnumerable())
@Html.PagedListPager((IPagedList)Model.AsEnumerable(), page => Url.Action("Index", new { page }))
答案 0 :(得分:1)
IEnumerable<EmployeeViewModel>
无法与IPagedList
直接投放到(IPagedList)Model.AsEnumerable()
,因为它们是不同的实例。您应该使用PagedList
方法返回ToPagedList
实例作为View
参数(假设employees
是List<EmployeeViewModel>
或视图模型数组):
public ActionResult Index(int? page)
{
var pageNumber = page ?? 1;
return View(employees.ToPagedList(pageNumber, 5));
}
并使用PagedListPager
内的绑定模型,如下所示:
@model PagedList.IPagedList<EmployeeViewModel>
@using PagedList.Mvc;
@Html.PagedListPager(Model, page => Url.Action("Index", new { page }))
并在IPagedList
中传递Model
到HtmlHelper.Partial
:
@Html.Partial("EmployeeList", Model)
类似问题:
How to load first x items and give user the option to load more in MVC.NET
答案 1 :(得分:0)
public ActionResult Index(int? page)
{
var pageNumber = page ?? 1;
return View(employees.ToList().ToPagedList(pageNumber, 5));
}
@using PagedList
@using PagedList.Mvc
@model IEnumerable <Employee>
@Html.Partial("EmployeeList", Model)
@Html.PagedListPager((IPagedList)Model, page => Url.Action("Index", new { page }))