// // GET: /Comment/Create public ActionResult Create(int id) { var comment = new Comment() { VideoID = id, }; return View(comment); }
public ActionResult Create(Comment comment) { if (ModelState.IsValid) { db.Comments.Add(comment); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.VideoID = new SelectList(db.Videos, "VideoID", "Name", comment.VideoID); return View(comment); }
public ActionResult Create(Comment comment) { if (ModelState.IsValid) //如果验证通过 { if (HttpContext.Request.IsAuthenticated) //如果用户已经登录 { //获取当前登录的用户的ID var userID = new Guid(Membership.GetUser().ProviderUserKey.ToString() ?? ""); comment.UserID = userID; } //设置要添加的评论添加时间为当前时间 comment.PostTime = DateTime.Now; db.Comments.Add(comment); //向数据库添加评论 db.SaveChanges(); } //返回评论列表 return RedirectToAction("Index", new { id=comment.VideoID}); }
public ActionResult Edit(Comment comment) { if (ModelState.IsValid) { db.Entry(comment).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.VideoID = new SelectList(db.Videos, "VideoID", "Name", comment.VideoID); return View(comment); }