我正在使用Page List ASP MVC插件向我的View添加分页,因为我的视图模型包含一个稍微失控的列表。但是,我遇到了一些问题。
List<T>
。如果我对数据库进行一次查询,我就可以将此列表的数据类型从List<ZipCodeTerritory>
更改为IPagedList<ZipCodeTerritory>
,然后只需按照文档从查询中加载列表,如下所示: / LI>
查看模型
public IPagedList<ZipCodeTerritory> zipCodeTerritory { get; set; }
控制器
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
.clear
使用的List
方法。其次,更重要的是,如果我需要执行不同的搜索,我会多次点击数据库并将每个查询的结果添加到zipCodeTerritory
列表中,我无法调用.addRange()
方法。有谁知道如何将项目添加到IPagedList
? 查看模型
public IPagedList<ZipCodeTerritory> zipCodeTerritory { get; set; }
控制器
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?)
}
List<T>
强制转换为IPagedList<T>
对象,但这也不起作用。这种方法可以解决我上面遇到的所有问题,所以如果有人知道如何简单地将List
转换为IPagedList
,那将非常感激。 查看模型
public IPagedList<ZipCodeTerritory> pagedTerritoryList { get; set; }
public List<ZipCodeTerritory> zipCodeTerritory { get; set; }
控制器
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]'.
答案 0 :(得分:4)
要将各种方法结合在一起,你可能应该做这样的事情(根据你的第一个例子):
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);