0

Im creating a simple blog using MVC and entity framework.

These are my two classes:

public class BlogPost
    {
        public int BlogPostID { get; set; }
        public string Title { get; set; }
        public string Post { get; set; }
        public DateTime Date { get; set; }

        public virtual ICollection<Comments> Comments { get; set; }
    }

public class Comments
    {
        public int CommentsID { get; set; }
        public string Comment { get; set; }
        public string Author { get; set; }
        public DateTime Date { get; set; }

        public int BlogPostID { get; set; }    
    }

Now, in my view, I display a Blogpost and i would like the user to be able to leave a comment, I imagin that the best way to do this is to create a beginform. This is what i have got:

@using (Html.BeginForm("AddComment", "BlogPosts", new { blogid = @Model.BlogPostID }, FormMethod.Post))
{

     @Html.TextBoxFor() <---How do i acess the "Author"-property in comments?
     @Html.TextAreaFor() <---How do i acess the "Comment"-property in comments?

    <button type="submit">Add Comment</button>
}

When the above problems are sorted out i´ll use this method to add the comment to the db:

public ActionResult AddComment(int blogid, Comments model)
        {

            BlogPost blogPost = db.BlogPosts.Find(blogid);

            blogPost.Comments.Add(model);

            db.SaveChanges();

            return RedirectToAction("Index");
        }

Does this seem like a good way to go about a task like this or am i maybe missing something fundamental? Thans!

1 Answer 1

1

Create a Partial view for adding comment the model for partial view will be Comments type

Now put below razor in your partial view

@model Comments

@using (Html.BeginForm("AddComment", "BlogPosts", new { blogid = @Model.BlogPostID }, FormMethod.Post))
{

     @Html.TextBoxFor(model=>model.Author) 
     @Html.TextAreaFor(model=>model.Comment) 

    <button type="submit">Add Comment</button>
}

Lets call this partial view AddComment.cshtml

Now in the blog post view add this partial view at the bottom like ,

@Html.Partial("AddComment",new Comments())
Sign up to request clarification or add additional context in comments.

4 Comments

If it works for you , mark as answer :) , let me know if you need any help.
Didnt works as i hoped the @Html.Partial got hit i got this error: The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.BlogPost_19DBFxxxxxxxxxE7DE2250882D7CADC8BBBE90B598B57372FFF801A', but this dictionary requires a model item of type 'SQL.Models.Comments'.
Use this @Html.Partial("AddComment",new Comments()) , see the edited version.
Append "in Asp.Net MVC" to question.

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.