[英]ERROR: Unable to cast object of type 'System.Collections.Generic.List' to type 'X.PagedList.IPagedList'
I use List instead of IEnumerable model 我使用List而不是IEnumerable模型
My Controller 我的控制器
public ActionResult Index(int? page)
{
var pageNumber = page ?? 1;
var itemCount = employees.ToPagedList(pageNumber, 5);
return View(employees.ToList());
}
My View 我的观点
@Html.Partial("EmployeeList", Model.AsEnumerable())
@Html.PagedListPager((IPagedList)Model.AsEnumerable(), page => Url.Action("Index", new { page }))
IEnumerable<EmployeeViewModel>
can't be directly cast to IPagedList
with (IPagedList)Model.AsEnumerable()
since they're different instances. IEnumerable<EmployeeViewModel>
无法使用(IPagedList)Model.AsEnumerable()
直接转换为IPagedList
,因为它们是不同的实例。 You should return a PagedList
instance using ToPagedList
method as View
argument (assumed employees
is an List<EmployeeViewModel>
or array of viewmodels): 您应该使用
ToPagedList
方法返回一个PagedList
实例作为View
参数(假定的employees
是List<EmployeeViewModel>
或viewmodel数组):
public ActionResult Index(int? page)
{
var pageNumber = page ?? 1;
return View(employees.ToPagedList(pageNumber, 5));
}
And use the bound model inside PagedListPager
like this: 并使用
PagedListPager
的绑定模型,如下所示:
@model PagedList.IPagedList<EmployeeViewModel>
@using PagedList.Mvc;
@Html.PagedListPager(Model, page => Url.Action("Index", new { page }))
And pass the IPagedList
through Model
in HtmlHelper.Partial
: 并通过
HtmlHelper.Partial
Model
传递IPagedList
:
@Html.Partial("EmployeeList", Model)
Similar issue: 类似的问题:
How to load first x items and give user the option to load more in MVC.NET 如何加载前x个项目,并为用户提供在MVC.NET中加载更多内容的选项
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 }))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.