我正在尝试将网格添加到我的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
@if (Model.HasPreviousPage)
{
@Html.ActionLink("<<", "ViewAll", new { page = 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter })
@Html.Raw(" ");
@Html.ActionLink("< Prev", "ViewAll", new { page = Model.PageNumber - 1, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter })
}
else
{
@:<<
@Html.Raw(" ");
@:< Prev
}
@if (Model.HasNextPage)
{
@Html.ActionLink("Next >", "ViewAll", new { page = Model.PageNumber + 1, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter })
@Html.Raw(" ");
@Html.ActionLink(">>", "ViewAll", new { page = Model.PageCount, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter })
}
else
{
@:Next >
@Html.Raw(" ")
@:>>
}
</div>
我已经检查了微软的Contosos University示例,但无法发现任何差异。其他人可以看到问题所在吗?
答案 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中内置的标准类型,因此您必须引用正确的程序集。