Reputation: 929
I am trying to create a simple blog application using asp.net. I am a novice and trying to teach myself c# and asp .net mvc.
The problem that I am facing is that when I try to add a comment to a post then it doesn't work. I do get a comment attached to the post but it doesn't display anything. Also, when I check the database - it just contains Null fields except for the BlogID field. What am I doing wrong?
My code is below:
Blog - class
public class Blog
{
public int BlogID { get; set; }
public string Title { get; set; }
public string Writer { get; set; }
[DataType(DataType.MultilineText)]
public string Excerpt { get; set; }
[DataType(DataType.MultilineText)]
public string Content { get; set; }
[DataType(DataType.Date)]
public DateTime PublishDate { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}
Comment - class
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 virtual int BlogID { get; set; }
public virtual Blog Blog { get; set; }
}
BlogDetailViewModel
public class BlogDetailsViewModel
{
public Blog Blog { get; set; }
public Comment Comment { get; set; }
}
Blog - Details controller
public ViewResult Details(int id)
{
Blog blog = db.Blogs.Find(id);
BlogDetailsViewModel viewModel = new BlogDetailsViewModel {Blog = blog};
return View(viewModel);
}
Blog Details - view
@model NPLHBlog.ViewModels.BlogDetailsViewModel
has code to display blogpost + comment form at end of blog details page below:
@using (Html.BeginForm("Create", "Comment"))
{
<input type="hidden" name="BlogID" value="@Model.Blog.BlogID" />
<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>
comment - create controller
public class CommentController : Controller
{
private NPLHBlogDb db = new NPLHBlogDb();
//
// GET: /Comment/
public ActionResult Create(Comment c)
{
Blog blog = db.Blogs.Single(b => b.BlogID == c.BlogID);
blog.Comments.Add(c);
db.SaveChanges();
return RedirectToAction("Details", "Blog", new { ID = c.BlogID });
}
Any help would be grateful.
Upvotes: 1
Views: 216
Reputation: 1069
I think you should add HttpPost attribute to the Create ActionResult method.
[HttpPost]
public ActionResult Create(Comment c)
{
Blog blog = db.Blogs.Single(b => b.BlogID == c.BlogID);
blog.Comments.Add(c);
db.SaveChanges();
return RedirectToAction("Details", "Blog", new { ID = c.BlogID });
}
Upvotes: 0
Reputation: 929
Found the answer ... I updated the create action of comment controller to:
public ActionResult Create(BlogDetailsViewModel viewModel)
{
Blog blog = db.Blogs.Single(b => b.BlogID == viewModel.Comment.BlogID);
Comment c = viewModel.Comment;
blog.Comments.Add(c);
db.SaveChanges();
return RedirectToAction("Details", "Blog", new { ID = c.BlogID });
}
Upvotes: 1
Reputation: 1039588
Try associating the Comment with the Blog before saving because the Comment instance that is passed as action argument has its Blog
property null:
public ActionResult Create(Comment c)
{
Blog blog = db.Blogs.Single(b => b.BlogID == c.BlogID);
blog.Comments.Add(c);
// associate the comment with the blog that you have retrieved
c.Blog = blog;
db.SaveChanges();
return RedirectToAction("Details", "Blog", new { ID = c.BlogID });
}
Upvotes: 0