public ActionResult ViewProduct(FormCollection fc) { var fullName = fc["txtName"]; var comment = fc["TxtComment"]; var rating = Convert.ToInt32(fc["slctRating"]); var productId = Convert.ToInt32(fc["ID"]); comment c = new comment(); c.FullName = fullName; c.Comments = comment; c.ThisDateTime = DateTime.Now; c.ProductID = productId; c.Rating = rating; db.comments.Add(c); db.SaveChanges(); var p = db.products.Where(x => x.ProductID == productId).FirstOrDefault(); var ave = db.comments.Where(x => x.ProductID == productId).Select(x => x.Rating).Average(); int?average = Convert.ToInt32(ave); p.AverageRating = average; db.SaveChanges(); ProductsCommentsModel pcm = new ProductsCommentsModel(); pcm.Product = db.products.Find(productId); pcm.CommentList = db.comments.Where(x => x.ProductID == productId).ToList(); return(View(pcm)); }
public ActionResult ViewProduct(int?id) { var p = db.products.Where(x => x.ProductID == id).FirstOrDefault(); var ave = db.comments.Where(x => x.ProductID == id).Select(x => x.Rating).Average(); int?average = Convert.ToInt32(ave); p.AverageRating = average; db.SaveChanges(); ProductsCommentsModel pcm = new ProductsCommentsModel(); pcm.Product = db.products.Find(id); pcm.CommentList = db.comments.Where(x => x.ProductID == id).ToList(); return(View(pcm)); }