ページ リストASP MVC プラグインを使用してビューにページングを追加しています。これは、ビュー モデルに少し制御不能になるリストが含まれているためです。ただし、いくつかの問題が発生しています。
- ビュー モデルには
List<T>
、別のモデル オブジェクトの が含まれています。データベースに対して 1 つのクエリを作成すると、このリストのデータ型を から に変更できます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
1 つには、C#が使用するメソッドを使用できませんList
。2 つ目は、さらに重要なことですが、別の検索を実行してデータベースに数回アクセスし、各クエリの結果を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
この方法は上記の問題をすべて解決するので、単純に aを に変換する方法を誰かが知っていれば、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]'.