1

I am developing a simple blog application to teach myself C# and asp .net mvc3. I am stuck at a stage where I need to update comments to a post.

Comment class in my code is as follows:

 public class Comment
    {
        public int CommentID { get; set; }
        public string Name { get; set; }

        [DataType(DataType.EmailAddress)]
        public string Email { get; set; }

        [DataType(DataType.MultilineText)]
        public string CommentBody { get; set; }

        public int BlogID { get; set; } 
        public virtual Blog Blog { get; set; }
    }

I have a comment form on the blog details page which takes the name, email and comment. The code is as follow:

    <div class="editor-label">
        @Html.LabelFor(model => model.Comment.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Comment.Name)
        @Html.ValidationMessageFor(model => model.Comment.Name)
    </div>  
    <div class="editor-label">
        @Html.LabelFor(model => model.Comment.Email)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Comment.Email)
        @Html.ValidationMessageFor(model => model.Comment.Email)
    </div>            

    <div class="editor-label">
        @Html.LabelFor(model => model.Comment.CommentBody)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Comment.CommentBody)
        @Html.ValidationMessageFor(model => model.Comment.CommentBody)
    </div>
    <p>
        <input type="submit" value="Add Comment" />
    </p>

I am not sure how to pass the blogid with this so that the comment gets updated with the correct blogid.

thanks.

1
  • What are you doing in your action? Can you add the action code? Commented Jun 29, 2012 at 8:53

2 Answers 2

3

You could use a hidden field inside the form:

@Html.HiddenFor(x => x.Comment.BlogID)
Sign up to request clarification or add additional context in comments.

8 Comments

introducing a hiddenform gives this error: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Comments_Blogs_BlogID". The conflict occurred in database "NPLHBlogDb", table "dbo.Blogs", column 'BlogID'. The statement has been terminated.
I guess that's because your Blog property is null. You will have to use the BlogID property to fetch from the database the actual Blog before inserting the comment.
I tried doing this @Html.HiddenFor(x => x.Comment.BlogID, Model.Blog.BlogID). This doesn't work. I can see that a null value is being passed to comment controller. However, I do not know how to pass the correct value.
You will have to fetch it from your database. If you wanted to populate the entire Blog property you will have to use hidden fields for each of its properties: @Html.HiddenFor(x => x.Comment.Blog.Prop1) and so on...
Apologies for being annoying but I am not getting it, I want to set the comment.BlogID to be equal to the id of the blog post the comment is being made.
|
1
@Html.HiddenFor(model => model.Comment.BlogID)

3 Comments

how will this pick up the correct BlogID for the new comment?
introducing a hiddenform gives this error: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Comments_Blogs_BlogID". The conflict occurred in database "NPLHBlogDb", table "dbo.Blogs", column 'BlogID'. The statement has been terminated.
Maybe you have something more than the Comment property in your model; the comment belongs to a blog, so you can add that blog reference in your model. Once you have it use an Html.Hidden, specify the name of the field and use the value of Blog.BlogID for it, instead of using Html.HiddenFor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.