//Returns the view that will produce a comment for a specific post //Takes in 'id' as a reference to the post that the comment relates to public ActionResult CreateComment(int Id) { CommentInputModel model = new CommentInputModel(); model.PostId = Id; return View(model); }
public ActionResult CreateComment(CommentInputModel model) { //Executes the following only when the data required for the comment is valid if (model != null && this.ModelState.IsValid) { //Gets the current user and stores it as a variable var currentUser = User.Identity.GetUserId(); //Assigns the values required to create a comment to their corresponding //variables in comment class var comment = new Comment() { content = model.Text, postId = model.PostId, userId = currentUser, user = db.Users.FirstOrDefault(c => c.Id.Equals(currentUser)) }; //Adds comment to database and saves the new state of the database this.db.Comments.Add(comment); this.db.SaveChanges(); //Notifies success and returns to Bloglist this.AddNotification("Comment Created Successfully", NotificationType.SUCCESS); return RedirectToAction("BlogList"); } this.AddNotification("Oops, Comment was not created", NotificationType.ERROR); return View(model); }