public ActionResult AddComment(SubmitCommentModel commentModel) { if (!User.Identity.IsAuthenticated) { return RedirectToAction("Index", "Home"); } if (ModelState.IsValid) { Movie movie = db.Movies.FirstOrDefault(x => x.Id == commentModel.MovieId); var user = db.Users.FirstOrDefault(u => u.UserName == User.Identity.Name); var username = this.User.Identity.GetUserName(); var userId = this.User.Identity.GetUserId(); Comment newComment = new Comment() { User = user, CommentText = commentModel.CommentText, Movie = movie, CreatedOn = DateTime.Now }; db.Comments.Add(newComment); db.SaveChanges(); var viewModel = new CommentViewModel() { Author = newComment.User.UserName, AuthorId = newComment.User.Id, CommentText = newComment.CommentText, CreatedOn = newComment.CreatedOn }; return PartialView("_CommentPartial", viewModel); } return new HttpStatusCodeResult(HttpStatusCode.BadRequest, ModelState.Values.First().ToString()); }
public ActionResult AddComment(int? id, string comment) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Movie movie = db.Movies.Find(id); if (movie == null) { return HttpNotFound(); } if (!User.Identity.IsAuthenticated) { return RedirectToAction("Index", "Home"); } var user = db.Users.FirstOrDefault(u => u.UserName == User.Identity.Name); Comment newComment = new Comment { CommentText = comment, Movie = movie, User = user, CreatedOn = DateTime.Now }; db.Comments.Add(newComment); movie.Comments.Add(newComment); db.SaveChanges(); return RedirectToAction("Details/" + movie.Id); }