2013-07-13 31 views
0

這裏是我的博文模型類:System.Collections.Generic.IEnumerable <MyBlogSite.Models.BlogPost>」不包含定義 '分類'

public class BlogPost 
{ 
    public int Id { get; set; } 
    public string Title { get; set; } 

    [AllowHtml] 
    public string ShortDescription { get; set; } 

    [AllowHtml] 
    public string PostBody { get; set; } 

    public string Meta { get; set; } 
    public string UrlSlug { get; set; }   
    public DateTime PostedOn { get; set; } 
    public DateTime? Modified { get; set; } 

    public virtual ICollection<BlogPostCategory> Categories { get; set; } 
    public virtual ICollection<BlogPostTag> Tags { get; set; } 
} 

這裏是我BlogPostCategory模型類:

public class BlogPostCategory 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public string UrlSlug { get; set; } 
    public string Description { get; set; } 

    // Decared virtual because the data must be returned from another table. 
    public virtual ICollection<BlogPost> BlogPosts { get; set; } 
} 

每個類屬於一個單獨的控制器/視圖。

最後,這裏是索引視圖爲博客頂部的端口:

@model IEnumerable<MyBlogSite.Models.BlogPost> 

@{ 
    ViewBag.Title = "Index"; 
} 

@Html.RenderPartial("~/Views/Category/_Categories.cshtml", Model.Categories); 

<p> 
    @Html.ActionLink("New Blog Post", "Create") 
</p> 
<table> 
<tr> 
    <th> 
     @Html.DisplayNameFor(model => model.Title) 
    </th> 
    .... 

在Model.Categories正在被傳遞的觀點是在那裏我得到這個職位的標題除外。在我看來,我已經在BlogPost模型中定義了類別。我究竟做錯了什麼?

回答

1

剃鬚刀頁面上的型號是IEnumerable<MyBlogSite.Models.BlogPost>。看起來你正試圖顯示有關集合中每個項目的信息。如果是這樣,那麼你可以遍歷它們或創建一個顯示/編輯器模板,並分別使用@Html.DisplayFor(x => x)@Html.EditorFor(x => x)

@foreach(var post in Model) { 
    <p>Do stuff here with the local "post" variable.</p> 
} 

這裏的a link to Scott Gu's blog談論剃刀意見@model指令。

相關問題