gpt4 book ai didi

asp.net - 错误 : Unable to cast object of type 'System.Collections.Generic.List' to type 'X.PagedList.IPagedList'

转载 作者:行者123 更新时间:2023-12-02 03:00:01 25 4
gpt4 key购买 nike

我使用 List 而不是 IEnumerable 模型

我的 Controller

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

最佳答案

IEnumerable<EmployeeViewModel> 不能用 IPagedList 直接转换为 (IPagedList)Model.AsEnumerable(),因为它们是不同的实例。您应该使用 PagedList 方法作为 ToPagedList 参数返回一个 View 实例(假设 employees 是一个 List<EmployeeViewModel> 或 View 模型数组):

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

然后像这样在 PagedListPager 中使用绑定(bind)模型:

@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

关于asp.net - 错误 : Unable to cast object of type 'System.Collections.Generic.List' to type 'X.PagedList.IPagedList' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46679767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com