本文介绍了无法添加到IPagedList对象或转移List< T>.到IPagedList< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用页面列表 ASP MVC插件向我的视图添加分页作为我的视图模型包含一个有时会失去控制的列表.但是,我遇到了一些问题.
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.
- 视图模型包含另一个模型对象的
List<T>
.如果我对数据库进行查询,则可以将此列表的数据类型从List<ZipCodeTerritory>
更改为IPagedList<ZipCodeTerritory>
,然后只需按照文档中的说明从查询中加载列表,如下所示:
- 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:
查看模型
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
- 但是,这带来了两个问题.首先,我无法使用C#
List
使用的.clear
方法.其次,更重要的是,如果我需要执行几次查找数据库的几次搜索并将每个查询的结果添加到zipCodeTerritory
列表中,则无法调用.addRange()
方法.有谁知道如何向IPagedList
添加项目? - 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
?
查看模型
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
,将不胜感激. - 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.
查看模型
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]'.
推荐答案
要将各种方法结合在一起,您可能应该执行以下操作(基于第一个示例):
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);
这篇关于无法添加到IPagedList对象或转移List< T>.到IPagedList< T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!