简体   繁体   English

列表不包含“ ToPagedList”的定义

[英]List does not contain a definition for 'ToPagedList'

I'm using the PagedList.Mvc NuGet Package for paging, In a View everything seems to works perfectly nice but in my controller this error occurs 我正在使用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?)

even though I defined the namespace like: 即使我将名称空间定义为:

using PagedList;
using PagedList.Mvc;

my code look like this: 我的代码如下所示:

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

I tried deleting and adding PagedList again via Package Manager Console. 我尝试通过程序包管理器控制台再次删除和添加PagedList。 But I can't seem to fix it. 但是我似乎无法修复它。 What could be causing this and how to fix it? 是什么原因导致的?如何解决? Hope you guys can give any suggestions. 希望你们能提出任何建议。 Thanks in advance! 提前致谢!

You're probably missing a "using xxxxxx" namespace. 您可能缺少“使用xxxxxx”命名空间。

using PagedList; 使用PagedList;

These are usually "extension methods"......and while they extend something that you probably already have a "using" statement for...the extension method itself is probably in another namespace (thus requires an additional "using" statement). 这些通常是“扩展方法” ......而当它们扩展某些内容时,您可能已经有一个“使用”语句,...扩展方法本身可能在另一个名称空间中(因此需要附加的“使用”语句) )。

APPEND 附加

You mention "I tried deleting and adding PagedList again via Package Manager Console" 您提到“我尝试通过程序包管理器控制台再次删除和添加PagedList”

When you do this, there is a drop down box for the target project. 执行此操作时,将为目标项目提供一个下拉框。 Make sure you have the correct csproj listed in the drop down box. 确保下拉框中列出了正确的csproj。

To make super sure you have the reference, open up your proj file (csproj) in notepad or notepad++ and look for the reference. 为了确保您具有参考,请在记事本或notepad ++中打开proj文件(csproj),然后查找参考。

First of all don't use ToList() ! 首先不要使用ToList() If you have like 10.000 records in database it will first load them to memory and then execute method ToPagedList 如果数据库中有10.000条记录,它将首先将它们加载到内存中,然后执行方法ToPagedList

If you look in pagedlist extensions definition then you will see that you can also use iqueryable 如果您查看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);

About yours problem, check if you have referenced pagedlist and pagedlist.mvc. 关于您的问题,请检查是否已引用pagedlist和pagedlist.mvc。 Also try rebuild project and check if any other errors are shown 也尝试重建项目并检查是否显示了其他任何错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM