简体   繁体   中英

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

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

using 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"

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.

To make super sure you have the reference, open up your proj file (csproj) in notepad or notepad++ and look for the reference.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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