public ActionResult CreateOk(CommentCreateModel model) { int bookId = (int)(this.Session["bookId"] ?? 0); if (ModelState.IsValid) { var username = this.User.Identity.GetUserName(); var userId = this.User.Identity.GetUserId(); //var user = this.Data.Users.Where(u => u.UserName == username).FirstOrDefault(); var book = this.Data.Books.All().Where(x => x.Id == bookId).FirstOrDefault(); //if (book == null) //{ // ModelState.AddModelError("Place", "Ivalid place id!"); //} //if (user == null) //{ // ModelState.AddModelError("User", "Ivalid user!"); //} Comment comment = new Comment() { Text = model.Text, AuthorId = userId, CreatedOn = DateTime.Now, }; book.Comments.Add(comment); this.Data.SaveChanges(); return PartialView("_Success"); } return PartialView("_Create", model); }
public ActionResult Create(Comment comment) { if (ModelState.IsValid) { db.Comments.Add(comment); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.AuthorId = new SelectList(db.Users, "Id", "UserName", comment.AuthorId); ViewBag.BookId = new SelectList(db.Books, "Id", "Title", comment.BookId); return View(comment); }
public ActionResult Edit(Comment comment) { if (ModelState.IsValid) { db.Entry(comment).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.AuthorId = new SelectList(db.Users, "Id", "UserName", comment.AuthorId); ViewBag.BookId = new SelectList(db.Books, "Id", "Title", comment.BookId); return View(comment); }