我正在使用Page List ASP MVC 插件将分页添加到我的视图中,因为我的视图模型包含一个有时会有点失控的列表。但是,我遇到了一些问题。
- 视图模型包含
List<T>
另一个模型对象。如果我对数据库进行一次查询,我可以将此列表的数据类型从 更改List<ZipCodeTerritory>
为IPagedList<ZipCodeTerritory>
,然后只需按照文档从查询中加载列表,如下所示:
查看模型
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
C#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>
对象,但这也不起作用。这种方法将解决我上述所有问题,所以如果有人知道如何简单地将 a 转换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]'.