2014-02-07 53 views
0
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]'.` 

請向我解釋,我是錯的,因爲其他的網頁我還沒有找到答案。

+1

你能後的'ToPagedList'方法? – Andrei

回答

0

它與事實有關,你的看法是強類型到IPagedList<Post>

@model PagedList.IPagedList<Webtion8.Models.Post> 

,而你試圖傳遞一個HashSet of Posts爲您Tags作用的結果。

因此,不是這樣的:

public ActionResult Tags(string id) 
{ 
     Tag tag = GetTag(id); 
     return View("Index", tag.Posts); 
} 

你應該這樣做:

public ActionResult Tags(string id) 
{ 
    Tag tag = GetTag(id); 
    //TODO define pageNumber and pageSize or pass them as parameters to your action method 
    return View("Index",tag.Posts.ToPagedList(pageNumber, pageSize)); 
} 
+0

非常感謝,我沒有注意。 – user3240336

+0

在我的情況下,這是因爲我有2個索引操作,其中一個用於獲取,另一個用於發佈。我的表單的方法後發送數據到後索引,所以數據處理不正確。 – Kazem

相關問題