Unable to add to IPagedList object or transfer List<T> to IPagedList<T>

NealR picture NealR · Oct 8, 2013 · Viewed 11.8k times · Source

I'm using the Page List ASP MVC plugin to add paging to my View as my view model contains a list that gets a little out of control at time. However, I'm running into a few issues.

  • The view model contains a List<T> of another model object. If I make one query to the database, I'm able to change the data type of this list from List<ZipCodeTerritory> to IPagedList<ZipCodeTerritory>, then simply follow the documentation to load the list from the query like so:

View Model

public IPagedList<ZipCodeTerritory> zipCodeTerritory { get; set; }

Controller

search.zipCodeTerritory = (from z in db.ZipCodeTerritory
                          where z.StateCode.Equals(search.searchState) &&
                                z.EffectiveDate >= effectiveDate
                         select z).ToList().ToPagedList(pageNumber, pageSize);

//This works but this only covers one of the three different searches that are 
//possibly from this Index page
  • However, this presents a couple issues. For one, I'm unable to use the .clear method a C# List uses. Second, and more importantly, if I need to perform a different search where I hit the database a couple times and add the result of each query to the zipCodeTerritory list, I'm unable to call the .addRange() method. Does anyone know how to add items to an IPagedList?

View Model

public IPagedList<ZipCodeTerritory> zipCodeTerritory { get; set; }

Controller

foreach (var zip in zipArray)
{
    var item = from   z in db.ZipCodeTerritory
                where  z.ZipCode.Equals(zip) &&
                        z.EffectiveDate >= effectiveDate &&
                        z.IndDistrnId.Equals(search.searchTerritory) &&
                        z.StateCode.Equals(search.searchState)
                select z;
    search.zipCodeTerritory.AddRange(item); //This line throws the following exception:

    //PagedList.IPagedList<Monet.Models.ZipCodeTerritory>' does not contain a 
    //definition for 'AddRange' and no extension method 'AddRange' accepting a first 
    //argument of type 'PagedList.IPagedList<Monet.Models.ZipCodeTerritory>' could be 
    //found (are you missing a using directive or an assembly reference?)
} 
  • I also tried simply leaving the code as it was prior to adding the paging, then just trying to cast the List<T> into an IPagedList<T> object, however this didn't work either. This method would solve all my above problems, so if anyone knows how to simply convert a List to and IPagedList that would be GREATLY appreciated.

View Model

    public IPagedList<ZipCodeTerritory> pagedTerritoryList { get; set; }
    public List<ZipCodeTerritory> zipCodeTerritory { get; set; }

Controller

search.pagedTerritoryList = (IPagedList<ZipCodeTerritory>)search.zipCodeTerritory;

//This threw the following exception at runtime: 
//Unable to cast object of type  
//System.Collections.Generic.List'1[Monet.Models.ZipCodeTerritory]' to type 
//'PagedList.IPagedList'1[Monet.Models.ZipCodeTerritory]'.

Answer

GalacticCowboy picture GalacticCowboy · Oct 8, 2013

To combine your various approaches together, you probably should do something like this (based on your first example):

var mySearch = (from z in db.ZipCodeTerritory
                      where z.StateCode.Equals(search.searchState) &&
                            z.EffectiveDate >= effectiveDate
                     select z);

// Other searches, whatever; use mySearch.AddRange() here

search.zipCodeTerritory = mySearch.ToPagedList(pageNumber, pageSize);