public ActionResult CreateOk(CommentModel model) { int placeId = (int)(this.Session["placeId"] ?? 0); if (ModelState.IsValid) { var context = new DataContext(); var username = this.HttpContext.User.Identity.Name; var user = context.Users.Where(u => u.UserName == username).FirstOrDefault(); var place = context.Places.Where(x => x.Id == placeId).FirstOrDefault(); if (place == null) { ModelState.AddModelError("Place", "Ivalid place id!"); } if (user == null) { ModelState.AddModelError("User", "Ivalid user!"); } Comment comment = new Comment() { Text = model.Text, User = user }; place.Comments.Add(comment); context.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.PlaceId = new SelectList(db.Places, "Id", "Name", comment.PlaceId); ViewBag.UserId = new SelectList(db.Users, "Id", "UserName", comment.UserId); return View(comment); }
public ActionResult Edit(Comment commentviewmodel) { if (ModelState.IsValid) { db.Entry(commentviewmodel).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(commentviewmodel); }
public ActionResult Edit(Comment comment) { if (ModelState.IsValid) { db.Entry(comment).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.PlaceId = new SelectList(db.Places, "Id", "Name", comment.PlaceId); ViewBag.UserId = new SelectList(db.Users, "Id", "UserName", comment.UserId); return View(comment); }