Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC 3 Model-binding and form fields
    primarykey
    data
    text
    <p>I have an entity called <code>Domain.Models.BlogPost</code> which contains the following properties:</p> <ul> <li>PostID</li> <li>Title</li> <li>Author</li> <li>PostedDate</li> <li>Body</li> </ul> <p>I also have an entity called <code>Domain.Models.PostComment</code> which contains the following properties:</p> <ul> <li>CommentID</li> <li>PostID</li> <li>Author</li> <li>Email</li> <li>Website</li> <li>Body</li> </ul> <p><code>BlogPost</code> contains many <code>PostComments</code>. A one to many relationship.</p> <p>Now I have a view like this (separated comment form from blog post code via html comment):</p> <pre><code>@model Domain.Models.BlogPost @using Domain.Models; @{ ViewBag.Title = "Post"; } &lt;div class="postTitle"&gt;@Model.Title&lt;/div&gt; &lt;div class="subInfo"&gt; Posted by @Model.Author on @Model.PostedDate.ToString("D") &lt;/div&gt; &lt;div class="postBody"&gt; @Html.Markdown(Model.Body) &lt;/div&gt; &lt;br /&gt; @Model.PostComments.Count Comment(s). &lt;div class="comments"&gt; @foreach (PostComment postComment in Model.PostComments) { Html.RenderPartial("PostComment", postComment); } &lt;/div&gt; &lt;!-- BELOW IS THE ADD COMMENT FORM --&gt; &lt;div id="addComment"&gt; @using (Html.BeginForm("AddComment", "Blog")) { &lt;text&gt; @Html.Hidden("PostID", Model.PostID)&lt;br /&gt; Name: @Html.TextBox("Author")&lt;br /&gt; Email: @Html.TextBox("Email")&lt;br /&gt; Website: @Html.TextBox("Website")&lt;br /&gt; Body: @Html.TextArea("Body")&lt;br /&gt; &lt;input type="submit" value = "Add Comment" /&gt; &lt;/text&gt; } &lt;/div&gt; @Html.ActionLink("Add Comment", "AddComment") </code></pre> <p>The problem is that because the comment form uses <code>@Html.TextBox("Author")</code> and <code>@Html.TextBox("Body")</code>, they are populated with data from the model, which also contains properties <code>Author</code> and <code>Body</code>. Any suggestions on how to fix this so these fields don't get values put in them when the page loads?</p> <p>I also tried creating a <code>BlogPostViewModel</code> and setting that as the model of the view and assigning the <code>BlogPost</code> property with my actual model:</p> <pre><code>public class BlogPostViewModel { public BlogPost BlogPost { get; set; } public PostComment NewComment { get; set; } } </code></pre> <p>Then I did <code>@Html.TextBoxFor(x =&gt; x.NewComment.Author)</code> but when the form posted to this action method:</p> <pre><code>public ActionResult AddComment(PostComment postComment) { // ... } </code></pre> <p><code>postComment</code> did not bind to the form values :/</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload