2011-01-28 105 views
2

MVC 3與關於局部視圖的Razor問題。MVC 3與關於局部視圖的剃刀問題

我有這樣的:

@model MvcGroupie.Models.Message 

@{ 
    ViewBag.Title = "Details"; 
} 

<h2>Details</h2> 

<fieldset> 
    <legend>Message</legend> 

    <div class="display-label">postCreator</div> 
    <div class="display-field">@Model.postCreator</div> 

    <div class="display-label">postDate</div> 
    <div class="display-field">@String.Format("{0:g}", Model.postDate)</div> 

    <div class="display-label">postSubject</div> 
    <div class="display-field">@Model.postSubject</div> 

    <div class="display-label">postBody</div> 
    <div class="display-field">@Model.postBody</div> 
</fieldset> 

    @Html.Partial("~/Views/Shared/replyPartial.cshtml") 
<p> 
    @if(Model.postCreator == User.Identity.Name) {@Html.ActionLink("Edit", "Edit", new { id=Model.postID } + " | ")} 
    @Html.ActionLink("Reply", "Reply", new { id=Model.postID }) |                 
    @Html.ActionLink("Back to List", "Index") 
</p> 

對於一個非常簡單的職位和答覆MVC應用程序IM與學習演奏。我不能得到一個部分顯示回覆:/

如果我添加部分我得到'MvcGroupie.Models.Message',但這個字典需要一個'MvcGroupie.Models.Reply'類型的模型項目。好的,你無法在同一頁面上使用差異模型?第一行以@model MvcGroupie.Models.Message開頭,所以我可以訪問model.postSubject等。但如果我想添加答覆,並有人能夠從同一頁面回覆它不允許它,他們將落入@model MvcGroupie.Models.Reply ...

好奇如何解決這個問題...我試過@ Html.Partial(「〜/ Views/Shared/replyPartial.cshtml」,Model.Reply),但它不認可Model.Reply ....

嚴重的障礙在我的學習方式有什麼幫助?

回答

4

當您調用渲染採用不同模型的局部視圖時,需要將該模型傳遞給該視圖。默認行爲是部分視圖將使用與調用它的視圖相同的模型,但這不適用於您的情況,因爲模型不同。

試試這個:

@Html.RenderPartial("~/Views/Shared/replyPartial.cshtml", Model.Replies) 

我假設你的消息對象有一個回覆屬性。如果消息沒有任何回覆,請不要忘記在局部視圖中進行空檢查。