本文介绍了列表不包含"ToPagedList"的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PagedList.Mvc NuGet程序包进行分页,在视图"中似乎一切工作都很好,但是在我的控制器中会发生此错误

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

'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;

我的代码如下:

[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.但是我似乎无法修复它.是什么原因造成的,以及如何解决?希望你们能提供任何建议.预先感谢!

I tried deleting and adding PagedList again via Package Manager Console. 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!

推荐答案

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

First of all don't use ToList()!If you have like 10.000 records in database it will first load them to memory and then execute method ToPagedList

如果您查看pagedlist扩展名定义,那么您会发现也可以使用iqueryable

If you look in pagedlist extensions definition then you will see that you can also use 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.另外,请尝试重建项目并检查是否还显示其他错误

About yours problem, check if you have referenced pagedlist and pagedlist.mvc. Also try rebuild project and check if any other errors are shown

这篇关于列表不包含"ToPagedList"的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 17:19