我正在用ASP.NET MVC 2創建一個博客,以便學習,並且可能在將來使用它。現在我的問題是在帖子中添加評論。我有張貼詳細信息強類型視圖顯示帖子,並且我正在呈現一個強類型部分視圖,該視圖具有添加新評論的表單。我正在試圖做的是這樣的:將設置值傳遞給部分視圖
<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<Blog.Models.Post>" %>
<h2>Details</h2>
<fieldset>
<legend>Fields</legend>
<div class="display-field"><h2><%: Model.Title %></h2></div>
<div class="display-field">
<p><%: Model.Summary %></p>
</div>
<div class="display-field">
<p><%: Model.Content %></p>
</div>
</fieldset>
<div id="comment-section">
<% foreach (var comment in Model.Comments)
{ %>
<div>
<h3><%: comment.Title %></h3>
<p><%: comment.DateAdded %></p>
<p><%: comment.Content %></p>
</div>
<% } %>
</div>
<div>
</div>
<% Html.RenderPartial("Comment/Add", new Blog.Models.Comment { PostID= Model.ID }); %>
<p>
<%: Html.ActionLink("Edit", "Edit", new { id = Model.ID }) %> |
<%: Html.ActionLink("Back to List", "Index") %>
</p>
而且添加PartialView是這個:
<% using (Html.BeginForm("Add", "Comment")) {%>
<%: Html.ValidationSummary(true)%>
<fieldset>
<legend>Add a comment</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Title)%>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Title)%>
<%: Html.ValidationMessageFor(model => model.Title)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Content)%>
</div>
<div class="editor-field">
<%: Html.TextAreaFor(model => model.Content, new { Style = "width:500px; height:150px;" })%>
<%: Html.ValidationMessageFor(model => model.Content)%>
</div>
<p>
<input type="submit" value="Add" />
</p>
</fieldset>
<% } %>
但在我CommentController當我得到發佈的評論對象,它以cero(0)作爲PostID來發布,所以我不能將它與它相應的Post進行綁定。有什麼想法嗎?
這工作。我使用ASP.NET MVC Html幫助程序:<%:Html.HiddenFor(model => model.PostID)%> – FelixMM 2011-01-13 20:37:54