列表不包含 'ToPagedList' 的定义
List does not contain a definition for 'ToPagedList'
我正在使用 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。但我似乎无法修复它。是什么原因造成的以及如何解决?希望大家可以给点建议。提前致谢!
您可能缺少 "using xxxxxx" 命名空间。
使用分页列表;
这些通常是 "extension methods"......虽然它们扩展了你可能已经有一个 "using" 语句的东西......扩展方法本身可能在另一个命名空间中(因此需要额外的 "using" 语句)。
追加
你提到"I tried deleting and adding PagedList again via Package Manager Console"
执行此操作时,会出现一个用于目标项目的下拉框。确保您在下拉框中列出了正确的 csproj。
为了确保您拥有参考,请在记事本或记事本++中打开您的项目文件 (csproj) 并查找参考。
首先不要用ToList()
!
如果数据库中有 10.000 条记录,它将首先将它们加载到内存中,然后执行方法 ToPagedList
如果您查看分页列表扩展定义,您会发现您也可以使用 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。也尝试重建项目并检查是否显示任何其他错误
我正在使用 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。但我似乎无法修复它。是什么原因造成的以及如何解决?希望大家可以给点建议。提前致谢!
您可能缺少 "using xxxxxx" 命名空间。
使用分页列表;
这些通常是 "extension methods"......虽然它们扩展了你可能已经有一个 "using" 语句的东西......扩展方法本身可能在另一个命名空间中(因此需要额外的 "using" 语句)。
追加
你提到"I tried deleting and adding PagedList again via Package Manager Console"
执行此操作时,会出现一个用于目标项目的下拉框。确保您在下拉框中列出了正确的 csproj。
为了确保您拥有参考,请在记事本或记事本++中打开您的项目文件 (csproj) 并查找参考。
首先不要用ToList()
!
如果数据库中有 10.000 条记录,它将首先将它们加载到内存中,然后执行方法 ToPagedList
如果您查看分页列表扩展定义,您会发现您也可以使用 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。也尝试重建项目并检查是否显示任何其他错误