我試着在我的項目中使用視圖模型,但我有此錯誤:模型產品類型PagedList.PagedList [],但本詞典需要類型的模型項目
The model item passed into the dictionary is of type 'PagedList.PagedList
1[ContosoUniversity.Models.Course]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[ContosoUniversity.ViewModels.CourseViewModel]'.
控制器:
public ActionResult Index(int? page)
{
int pageNumber = page ?? 1;
var courses = courseService.GetAll();
var coursesViewList = Mapper.Map<IEnumerable<Course>, IEnumerable<CourseViewModel>>(courses);
var model = coursesViewList.ToPagedList(pageNumber, PageSize);
return View(model);
}
查看:
@model IEnumerable<ContosoUniversity.ViewModels.CourseViewModel>
@using PagedList.Mvc;
@using PagedList;
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Credits)
</th>
<th>
Students
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Credits)
</td>
<td>
@string.Join(",", item.Enrollments.Select(e => e.Student.FullName))
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.CourseID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.CourseID })
</td>
</tr>
}
</table>
@Html.PagedListPager((IPagedList)Model, page => Url.Action("Index", new { page }))
可能有人幫我解決一個問題好嗎?
錯誤是不言自明的,請嘗試 明白它。在你的控制器中放置斷點並檢查各種變量的內容。 – CodeCaster 2014-11-06 10:38:59