简体   繁体   English

错误:无法将类型为“System.Collections.Generic.List”的对象强制转换为“X.PagedList.IPagedList”

[英]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参数(假定的employeesList<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中加载更多内容的选项

**Controller Action ** **控制器动作**

    public ActionResult Index(int? page)
        { 
            var pageNumber = page ?? 1;            
            return View(employees.ToList().ToPagedList(pageNumber, 5));
        }


View page 查看页面

    @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.

相关问题 无法转换类型为&#39;System.Collections.Generic.List的对象 - Unable to cast object of type 'System.Collections.Generic.List 错误“无法将'System.Int32'类型的对象强制转换为'System.Collections.Generic.List`1 [System.Int32]'' - error “Unable to cast object of type 'System.Int32' to type 'System.Collections.Generic.List`1[System.Int32]'” 无法将“System.String”类型的对象转换为“System.Collections.Generic.List`1[System.String]”类型 - Unable to cast object of type 'System.String' to type 'System.Collections.Generic.List`1[System.String]' 无法将类型为“System.Collections.Generic.List”的对象强制转换为“System.Data.DataSet”类型 - Unable to cast object of type 'System.Collections.Generic.List to type 'System.Data.DataSet' InvalidCastException:无法将类型为“ CMP.Helpers.NavBarSettings”的对象转换为类型为“ System.Collections.Generic.List`1 [CMP.Helpers.NavBarItem]” - InvalidCastException: Unable to cast object of type 'CMP.Helpers.NavBarSettings' to type 'System.Collections.Generic.List`1[CMP.Helpers.NavBarItem]' 无法将类型&#39;object [] []&#39;转换为&#39;System.Collections.Generic.List <object> “ - Cannot convert type 'object[][]' to 'System.Collections.Generic.List<object>' 无法将类型&#39;System.Collections.Generic.List&#39;隐式转换为&#39;System.Collections.Generic.List - Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List 无法隐式转换类型System.Collections.Generic.List - Cannot implicitly convert type System.Collections.Generic.List 传递到 ViewDataDictionary 的 model 项的类型为“System.Collections.Generic.List” - The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List` 无法将类型&#39;System.Collections.Generic.List &lt;&gt;&#39;隐式转换为&#39;&#39; - Cannot implicitly convert type 'System.Collections.Generic.List<>' to ''
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM