2012-03-19 2 views
1

Я пытаюсь добавить сетку в таблицу данных в моем приложении MVC, но получаю следующее сообщение об ошибке:Добавление сетки ASP.NET 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> 

Я проверил пример Microsoft 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

Смежные вопросы