开发者

Pass Model with set values to PartialView

I'm building a blog with ASP.NET MVC 2 for fun, to learn and maybe to use it latter in the future. Right now my problem is adding a comment to a post. I have the post Details Strongly Typed view displaying the post, and I'm rendering a Strongly Typed PartialView that has the Form to add a new comment. What I'm trying to do is this:

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<Blog.Models.Post>" %>

<h2>Details</h2>

<fieldset>
    <legend>Fields</legend>
开发者_StackOverflow社区
    <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>

And the Add PartialView is this one:

<% 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>

<% } %>

But in my CommentController when I get the posted Comment object, it comes with cero (0) as a PostID, so I can't bind it with it's corresponding Post. Any thoughts?


You do not have the PostID on the form for adding a new comment. The only data MVC has available to it is the data sent from the client (form data/url data, etc). You can either had a hidden field or look at the referrer of the current request and parse out the PostID assuming it's on the URL. I would opt for the hidden field.


Add the post ID as a hidden field...

<input type="hidden" name="PostID" value="<%: Model.ID %>" />


What does the form action url look like? You probably need to add the id routeValue...

<% using (Html.BeginForm("Add", "Comment", new { id = Model.id })) {%>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜