【问题标题】:List does not contain a definition for 'ToPagedList'列表不包含“ToPagedList”的定义
【发布时间】:2015-11-04 01:49:13
【问题描述】:

我正在使用 PagedList.Mvc NuGet 包进行分页,在视图中一切似乎都运行良好,但在我的控制器中发生此错误

'System.Collections.Generic.List<CreateSocial.Models.UserProfiles>' does not contain a definition for 'ToPagedList' and no extension method 'ToPagedList' accepting a first argument of type 'System.Collections.Generic.List<CreateSocial.Models.UserProfiles>' could be found (are you missing a using directive or an assembly reference?)

即使我定义了这样的命名空间:

using PagedList;
using PagedList.Mvc;

我的代码如下所示:

[HttpGet]
public ActionResult Results(SearchViewModel svm, int CurrentPage)
{
    const int ResultsPerPage = 50;
    List<UserProfiles> results = db.UserProfiles.Where(w => w.FirstName.Contains(svm.SearchStr) || w.LastName.Contains(svm.SearchStr)).ToList();

    return View(results.ToPagedList(CurrentPage, ResultsPerPage));
}

我尝试通过包管理器控制台再次删除和添加 PagedList。但我似乎无法修复它。什么可能导致这种情况以及如何解决?希望大家能给点建议。提前致谢!

【问题讨论】:

    标签: c# asp.net-mvc namespaces nuget-package pagedlist


    【解决方案1】:

    首先不要使用ToList()! 如果您在数据库中有 10.000 条记录,它将首先将它们加载到内存中,然后执行方法 ToPagedList

    如果你查看 pagedlist 扩展定义,你会发现你也可以使用 iqueryable

        // Summary:
        //     Creates a subset of this collection of objects that can be individually accessed
        //     by index and containing metadata about the collection of objects the subset
        //     was created from.
        //
        // Parameters:
        //   superset:
        //     The collection of objects to be divided into subsets. If the collection implements
        //     System.Linq.IQueryable<T>, it will be treated as such.
        //
        //   pageNumber:
        //     The one-based index of the subset of objects to be contained by this instance.
        //
        //   pageSize:
        //     The maximum size of any individual subset.
        //
        // Type parameters:
        //   T:
        //     The type of object the collection should contain.
        //
        // Returns:
        //     A subset of this collection of objects that can be individually accessed
        //     by index and containing metadata about the collection of objects the subset
        //     was created from.
    public static IPagedList<T> ToPagedList<T>(this IQueryable<T> superset, int pageNumber, int pageSize);
    

    关于你的问题,检查你是否引用了 pagedlist 和 pagedlist.mvc。也尝试重建项目并检查是否显示任何其他错误

    【讨论】:

    • 这里非常重要。如果您使用ToList(),那么您将打败整个分页点。除了使用IQueryable,您还可以通过StaticPagedList 构造一个PagedList 实例,这将允许您传入通过SkipTake 检索到的子集(您可以拨打ToList() on)和总数。无论哪种方式,您都只查询当前页面实际需要的记录。
    【解决方案2】:

    您可能缺少“使用 xxxxxx”命名空间。

    使用分页列表;

    这些通常是“扩展方法”......虽然它们扩展了一些你可能已经有一个“使用”语句的东西......扩展方法本身可能在另一个命名空间中(因此需要一个额外的“ using" 语句)。

    追加

    您提到“我尝试通过包管理器控制台再次删除和添加 PagedList”

    执行此操作时,目标项目会出现一个下拉框。确保下拉框中列出了正确的 csproj。

    为确保您有参考,请在记事本或 notepad++ 中打开您的 proj 文件 (csproj) 并查找参考。

    【讨论】:

    • 我附上了我的答案。
    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多