2016-08-03 9 views
0

PagedListを使用せずにインデックスビューでページングと並べ替えを実行しようとしています。しかし、私はエラーに遭遇しています -辞書に渡されたモデル項目は 'System.Collections.Generic.List`型です

辞書に渡されるモデルアイテムは種類System.Collections.Generic.List`1 [AQB_MON.Models.DeviceLog]」であるが、この辞書は、タイプのモデルアイテムが必要です'AQB_MON.ViewModels.DeviceLogIndex'。

私は独自のViewModelを作成して、パラメータをCreate Viewに渡しました。 DeviceLog Index Viewを入力すると、特定のDeviceID用ですが、このDeviceIDをCreateビューに渡したいのですが、PagedListを使用していくつかの問題が発生しました。だから私は上記のエラーに遭遇する代わりに見つけた。

マイDeviceLogIndex ViewModelには、で構成されています - 私の見解の最初の部分がある

public class PagingInfo 
{ 
    public string SortField { get; set; } 
    public string SortDirection { get; set; } 
    public int PageSize { get; set; } 
    public int PageCount { get; set; } 
    public int CurrentPageIndex { get; set; } 
} 

- -

ページングやソートのために使用されている
public class DeviceLogIndex 
{ 
    public DeviceLogIndex() 
    { 
     this.DeviceLogs = new List<DeviceLog>(); 
    } 
    public int DeviceID { get; set; } 
    public string mytest { get; set; } 
    public List<DeviceLog> DeviceLogs { get; set; } 

と私のPageInfoのViewModelさ

@model AQB_MON.ViewModels.DeviceLogIndex 

@{ 
ViewBag.Title = "Index"; 
} 
@{ 
AQB_MON.ViewModels.PagingInfo info = ViewBag.PagingInfo; 
} 

「info」宣言が問題を引き起こしていると思いますか?

public ActionResult Index(int id) 
{ 
    var devicelogs = db.DeviceLogs 
     .Include(d => d.Device) 
     .Include(d => d.DeviceLogType) 
     .Include(d => d.Device.ManufacturerModel) 
     .Where(d => d.DeviceID == id); 
    { 
     PagingInfo info = new PagingInfo(); 
      info.SortField = "DeviceLog"; 
      info.SortDirection = "ascending"; 
      info.PageSize = 15; 
      info.PageCount = Convert.ToInt32(Math.Ceiling((double)(db.DeviceLogs.Count()/info.PageSize))); 
      info.PageCount +=1; 
      info.CurrentPageIndex = 0; 
      var query = devicelogs.OrderBy(c => c.DeviceLogID).Take(info.PageSize); 
      ViewBag.PagingInfo = info; 
      List<DeviceLog> model= query.ToList(); 

     return View(model); 
    } 
} 

[HttpPost] 
public ActionResult Index(PagingInfo info, int id) 
{ 
    var devicelogs = db.DeviceLogs 
     .Include(d => d.Device) 
     .Include(d => d.DeviceLogType) 
     .Include(d => d.Device.ManufacturerModel) 
     .Where(d => d.DeviceID == id); 
    { 
     IQueryable<DeviceLog> query = null; 
     switch(info.SortField) 
     { 
      case "EntryDate": 
       query = (info.SortDirection == "ascending" ? 
        devicelogs.OrderBy(c => c.EntryDate) : 
        devicelogs.OrderByDescending(c => c.EntryDate)); 
       break; 
      case "LogType": 
       query = (info.SortDirection == "ascending" ? 
        devicelogs.OrderBy(c => c.DeviceLogType) : 
        devicelogs.OrderByDescending(c => c.DeviceLogType)); 
       break; 
     } 

     query = query.Skip(info.CurrentPageIndex * info.PageSize).Take(info.PageSize); 
     ViewBag.PagingInfo = info; 
     List<DeviceLog> model = query.ToList(); 
     return View(model); 
    } 
} 
+0

また、答えは、ビューバックと一緒にPagingInfoを渡すのではなく、ViewModelクラスがより良い構造のためにページングクラスを継承していることを考慮してください。 –

+0

私は問題を過ぎているが、ページングとソートは機能していない。これは問題だろうか?私はあなたの提案についてどうやって行くのか分かりません。 –

答えて

2

あなたのビューが強くDeviceLogIndexに入力されていますが、あなたのアクションメソッドからは、ビューにList<DeviceLog>を渡している -

私のコントローラがあります。これが不一致エラーを起こす理由です。

この問題を解決するには、DeviceLogIndexクラスのオブジェクトを作成し、LINQクエリ結果からDeviceLogsプロパティ値を割り当てる必要があります。 DeviceLogIndexオブジェクトをビューに渡します。

DeviceLogIndex vm = new DeviceLogIndex(); 

//your existing code goes here 

var query = devicelogs.OrderBy(c => c.DeviceLogID).Take(info.PageSize); 
vm.DeviceLogs=query.ToList(); 
return View(vm); 

Model.DeviceLogsにアクセスして、各デバイス項目にアクセスできます。

@model AQB_MON.ViewModels.DeviceLogIndex 
@foreach(var item in Model.DeviceLogs) 
{ 
    <p>@item.DeviceLogID</p> 
} 
+0

ありがとうございます、これは私のエラーを過ぎて今私はちょうどページングを取得し、作業をソートする必要があります:) –

2

あなたが代わりに行う必要がどのようなタイプList<DeviceLog>

を渡しているがあなたのビューはタイプDeviceLogIndexを期待している例についてDeviceLogIndex

List<DeviceLog>を割り当てることです:

DeviceLogIndex model = new DeviceLogIndex(); 
model.DeviceLogs = youDeviceLoglist; 
return View(model); 

あなたのビューで:

@foreach (var dl in Model.DeviceLogs) 
{ 
    //Do something 
} 
関連する問題