错误:无法转换类型&System; System.Collections.Generic.List'输入' X.PagedList.IPagedList'

时间:2017-10-11 04:20:02

标签: asp.net asp.net-mvc asp.net-mvc-5 pagedlist

我使用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 }))

2 个答案:

答案 0 :(得分:1)

IEnumerable<EmployeeViewModel>无法与IPagedList直接投放到(IPagedList)Model.AsEnumerable(),因为它们是不同的实例。您应该使用PagedList方法返回ToPagedList实例作为View参数(假设employeesList<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中传递ModelHtmlHelper.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 }))