1

我正在尝试将网格添加到我的 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示例,但没有发现任何差异。其他人可以看到问题可能是什么吗?

4

1 回答 1

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);
}

我从这里使用了扩展方法。这IPagedList<T>不是 ASP.NET MVC 中内置的标准类型,因此您必须引用正确的程序集。

于 2012-03-19T19:51:28.857 回答