public ActionResult Index(int? page)
{
var model = db.Posts.ToList();
int pageNumber = page ?? 1;
int pageSize = 10;
return View(model.ToPagedList(pageNumber, pageSize));}
「索引」中我有一個帶有標籤的框,它需要名稱爲標籤並進入「變量」傳入詞典的模型項目類型爲'a',但此字典需要一個'b'類型的模型項目
(@foreach (Webtion8.Models.Tag tag in item.Tags){
<span><a href="@Href("~/Post/Tags/"+tag.Name)">@tag.Name</a></span>}):
@model PagedList.IPagedList<Webtion8.Models.Post>
@using PagedList.Mvc
@{
ViewBag.Title = "Index";
}
<link href="@Url.Content("/Content/PagedList.css")" rel="stylesheet" type="text/css" />
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
@foreach (var item in Model)
{<tr>
<td>
post: @Html.DisplayFor(modelItem => item.Title)
</td>
<td>
date: @Html.DisplayFor(modelItem => item.DateTime)
</td>
<td>
avtor: @Html.DisplayFor(modelItem => item.Avtor)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
<tr><td colspan="4"> tegs:
@foreach (Webtion8.Models.Tag tag in item.Tags)
{
<span><a href="@Href("~/Post/Tags/"+tag.Name)">@tag.Name</a></span>
}
</td></tr>
<tr></tr>
<tr>
<td colspan="4">
@Html.DisplayFor(modelItem => item.Body)</td>
</tr>
<tr><td colspan="4"></td></tr>
}</table>
<div style="text-align: center;">
@Html.PagedListPager(Model, page => Url.Action("Index", new { page }),
new PagedListRenderOptions {
LinkToFirstPageFormat = "<<",
LinkToPreviousPageFormat = "<",
LinkToNextPageFormat = ">",
LinkToLastPageFormat = ">>" })
</div>
他們到這裏
public ActionResult Tags(string id)
{ Tag tag = GetTag(id);
return View("Index", tag.Posts);}
,並在這裏開始我也解決不了問題。從理論上講,這段代碼應該返回帶有我們點擊的標籤樣例博客條目的頁面索引。但是,該方案提供了一個錯誤
The model item passed into the dictionary is of type 'System.Collections.Generic.HashSet`1[Webtion8.Models.Post]', but this dictionary requires a model item of type 'PagedList.IPagedList`1[Webtion8.Models.Post]'.
和
System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.HashSet`1[Webtion8.Models.Post]', but this dictionary requires a model item of type 'PagedList.IPagedList`1[Webtion8.Models.Post]'.`
請向我解釋,我是錯的,因爲其他的網頁我還沒有找到答案。
你能後的'ToPagedList'方法? – Andrei