2012-03-19 6 views
1

を使用して、アプリケーションにASP.NET MVCグリッドを追加:は、私は私のMVCアプリケーション内のデータのテーブルにグリッドを追加しますが、次のエラーメッセージが出続けるしようとしていますPagedList

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[AssociateTracker.Models.Associate]', but this dictionary requires a model item of type 'PagedList.IPagedList`1[AssociateTracker.Models.Associate]'. 

ビュー:

@model PagedList.IPagedList<AssociateTracker.Models.Associate> 

@{ 
    ViewBag.Title = "ViewAll"; 
} 

<h2>View all</h2> 

<table> 
    <tr> 
     <th>First name</th> 
     <th>@Html.ActionLink("Last Name", "ViewAll", new { sortOrder=ViewBag.NameSortParm, currentFilter=ViewBag.CurrentFilter })</th> 
     <th>Email address</th> 
     <th></th> 
    </tr> 

@foreach (var item in Model) 
{ 
    <tr> 
     <td>@item.FirstName</td> 
     <td>@item.LastName</td> 
     <td>@item.Email</td> 
     <td> 
      @Html.ActionLink("Details", "Details", new { id = item.AssociateId }) | 
      @Html.ActionLink("Edit", "Edit", new { id = item.AssociateId }) | 
      @Html.ActionLink("Delete", "Delete", new { id=item.AssociateId }) 
     </td> 
    </tr> 
} 
</table> 

<div> 
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) 
    of @Model.PageCount 
    &nbsp; 
    @if (Model.HasPreviousPage) 
    { 
     @Html.ActionLink("<<", "ViewAll", new { page = 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter }) 
     @Html.Raw("&nbsp;"); 
     @Html.ActionLink("< Prev", "ViewAll", new { page = Model.PageNumber - 1, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }) 
    } 
    else 
    { 
     @:<< 
     @Html.Raw("&nbsp;"); 
     @:< Prev 
    } 
    &nbsp; 
    @if (Model.HasNextPage) 
    { 
     @Html.ActionLink("Next >", "ViewAll", new { page = Model.PageNumber + 1, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }) 
     @Html.Raw("&nbsp;"); 
     @Html.ActionLink(">>", "ViewAll", new { page = Model.PageCount, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }) 
    } 
    else 
    { 
     @:Next > 
     @Html.Raw("&nbsp;") 
     @:>> 
    } 
</div> 

私はマイクロソフトのContosos Universityの例をチェックしましたが、違いは見つかりません。誰かがその問題が何であるかを見ることができますか?

+0

コントローラで渡しているモデルが、ビューで指定したモデルと一致しません。 – jacqijvv

+0

感謝私のコントローラをチェックするつもりはありませんでした。それが問題の場所です! –

答えて

3

エラーメッセージはかなりわかりやすいようです。あなたのビューはIPagedList<Associate>インスタンスを想定していますが、コントローラアクションからList<Associate>を渡しています。

だからあなたのコントローラのアクション内では、ビューに適切なモデルを提供する必要があります。

public ActionResult Index(int? page) 
{ 
    List<Associate> associates = GetAssociates(); 
    IPagedList<Associate> model = associates.ToPagedList(page ?? 1, 10); 
    return View(model); 
} 

私は拡張メソッドfrom hereを使用。 IPagedList<T>は、ASP.NET MVCで構築された標準タイプではないため、適切なアセンブリを参照する必要があります。

+0

いつも偉大な答えとしてダーリン! – jacqijvv

関連する問題