public ActionResult AddComment(int id) { var user = _db.Users.FirstOrDefault(x => x.UserName.Equals(User.Identity.Name)); if (user != null && user.Reviews.Any(x => x.Product.Id == id)) { return RedirectToAction("ProductDetail", "ViewProduct", new { id }); } var firstOrDefault = _db.Products.FirstOrDefault(x => x.Id == id); var productName = firstOrDefault.Name; var model = new CommentViewModel { ProductName = productName, Id = firstOrDefault.Id }; return View(model); }
public ActionResult AddComment(CommentViewModel model) { if (this.ModelState.IsValid) { var product = _db.Products.FirstOrDefault(x => x.Id == model.Id); var user = _db.Users.FirstOrDefault(x => x.UserName.Equals(User.Identity.Name)); var rating = new Review { AddDate = DateTime.Now, Rating = model.Rating, Comment = model.Comment, Product = product, User = user }; _db.Reviews.Add(rating); _db.SaveChanges(); return RedirectToAction("ProductDetail", "ViewProduct", new { model.Id }); } return View(); }