Добавление сетки ASP.NET MVC в приложение с использованием PagedList - PullRequest
1 голос
/ 19 марта 2012

Я пытаюсь добавить сетку в таблицу данных в своем приложении MVC, но получаю следующее сообщение об ошибке:

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 , но не вижу никаких отличий.Кто-нибудь еще может понять, в чем проблема?

1 Ответ

3 голосов
/ 19 марта 2012

Сообщение об ошибке кажется довольно понятным. Ваше представление ожидает IPagedList<Associate> экземпляр, но вы передаете List<Associate> от действия вашего контроллера.

Таким образом, внутри действия вашего контроллера вы должны предоставить правильную модель для представления:

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

Я использовал метод расширения отсюда . IPagedList<T> не является стандартным типом, встроенным в ASP.NET MVC, поэтому вам придется ссылаться на соответствующие сборки.

...