public ActionResult _CreateComment(Models.ProductComment productComment) { if (ModelState.IsValid) { productComment.CreateDate = System.DateTime.Now; DatabaseContext.ProductComments.Add(productComment); DatabaseContext.SaveChanges(); return(PartialView("_ShowComment", DatabaseContext.ProductComments.Where(current => current.ProductId == productComment.ProductId).ToList())); } return(PartialView(productComment)); }
[Authorize] //登入會員才可留言 public ActionResult AddComment(int id, string Content) { //取得目前登入使用者Id var userId = HttpContext.User.Identity.GetUserName(); var currentDateTime = DateTime.Now; var comment = new Models.ProductComment() { ProductId = id, Content = Content, UserId = userId, CreateDate = currentDateTime }; using (Models.ShoppingCartsEntities db = new Models.ShoppingCartsEntities()) { db.ProductComments.Add(comment); db.SaveChanges(); } return(RedirectToAction("Details", new { id = id })); }
[HttpPost] //限定使用POST public ActionResult AddComment(int id, string Content) { //取得目前登入使用者Id var userId = String.Format("{0}", Session["LoginName"]); var currentDateTime = DateTime.Now; var comment = new Models.ProductComment() { ProductId = id.ToString(), Content = Content, UserId = userId, CreateDate = currentDateTime.ToString() }; using (Models.CartsEntities db = new Models.CartsEntities()) { db.ProductComments.Add(comment); db.SaveChanges(); } return(RedirectToAction("Details", new { id = id })); }