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

情到浓时终转凉″ 提交于 2019-12-24 01:24:46

问题


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

回答1:


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).ToList();

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

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


来源:https://stackoverflow.com/questions/19254055/unable-to-add-to-ipagedlist-object-or-transfer-listt-to-ipagedlistt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!