问题
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 fromList<ZipCodeTerritory>
toIPagedList<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 thezipCodeTerritory
list, I'm unable to call the.addRange()
method. Does anyone know how to add items to anIPagedList
?
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 anIPagedList<T>
object, however this didn't work either. This method would solve all my above problems, so if anyone knows how to simply convert aList
to andIPagedList
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