2013-03-25 71 views
0

目前我有一個局部視圖,顯示與某個blogid關聯的數據庫中的評論。我無法得到這個爲我的生活工作。我甚至無法弄清楚如何將blogid傳遞給partial,所以我拉出正確的評論。這是我到目前爲止。在博客上顯示評論發佈信息

/博客/詳細註釋部分

<h2>Comments</h2> 
<hr/> 
@if (!User.Identity.IsAuthenticated) 
{ 
    <p>You Must Be Logged In To Comment.</p> 
} 
else 
{ 
    @: Posting comments as @User.Identity.Name<hr/> 
    @Html.Action("Create", "Comment", new {blogId = Model.Id}); 
    @: <hr/> 
} 
@Html.Partial("_Comments") 

_comments局部視圖

@model IEnumerable<GregG.Models.Comment> 

@foreach (var item in Model) { 
    if (item.BlogId == 1) //I need to make this dynamic but can't figure out how to pass the id 
    { 
     <small>Posted by: @Html.DisplayFor(modelItem => item.UserName) on:     @Html.DisplayFor(modelItem => item.PostedDate)</small> 
     @Html.DisplayFor(modelItem => item.Meat)<hr/> 
    } 
} 

我的第一個問題是:我如何通過我的博客ID給部分。 我的第二個問題是爲什麼我在我的部分行上收到此錯誤。

傳遞到詞典中的模型產品類型「System.Data.Entity.DynamicProxies.Blog_18032B13AD6163845F0AE57E827683E3638FD813CA4EE066F28D05E5406E633D」,但這需要字典類型的模型項「System.Collections.Generic.IEnumerable`1 [格雷格。 Models.Comment]」。

在此先感謝您的幫助。

回答

1

有幾個過載的Patial方法支持指定視圖模型的空間。如果您要顯示的評論已經成爲當前視圖模型的一部分,請使用此選項。

@Html.Partial("_Comments", Model.Comments) 

但是,如果他們不是您的視圖模型的一部分,我建議您在CommentController像這樣創建一個單獨的行動:

Action List(int blogId) 
{ 
    // query comments from database 
    var model = db.Comments.Where(c => c.BlogId == blogId); 
    return View(model); 
} 

從你/Blog/Details.cshtml文件調用它像這樣:

@Html.Action("List", "Comment", new { blogId = Model.Id }) 
+0

我結束了使用@ Html.Action選項,它的工作完全謝謝! –