public ActionResult Comment(CommentSubmitViewModel comment) { if (comment != null && ModelState.IsValid) { var currentTicket = this.Data.Tickets.GetById(comment.TicketId); var newComment = new Comment() { Content = comment.Content, Ticket = currentTicket, UserId = comment.AuthorId }; currentTicket.Comments.Add(newComment); this.Data.SaveChanges(); var commentViewModel = new CommentViewModel() { AuthorName = User.Identity.GetUserName(), Content = comment.Content }; return PartialView("_CommentPartial", commentViewModel); } ModelState.AddModelError("Content", "Content should be atleast 6 characters long."); return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest, ModelState.Values.First().ToString()); }
public ActionResult Create(Comment comment) { if (ModelState.IsValid) { db.Comments.Add(comment); db.SaveChanges(); return RedirectToAction("Index"); } return View(comment); }
public ActionResult Edit(Comment comment) { if (ModelState.IsValid) { this.Data.Comments.Update(comment); this.Data.SaveChanges(); return RedirectToAction("Index"); } ViewBag.TicketId = new SelectList(this.Data.Tickets.All(), "Id", "AuthorId", comment.TicketId); ViewBag.UserId = new SelectList(this.Data.Users.All(), "Id", "UserName", comment.UserId); return View(comment); }
public ActionResult Edit(Comment comment) { if (ModelState.IsValid) { db.Entry(comment).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(comment); }
public ActionResult CreateComment(int ticketId, CommentViewModel comment) { if (ModelState.IsValid) { var ticket = this.Data.GetRepository<Ticket>().GetById(ticketId); if (ticket != null) { string userId = User.Identity.GetUserId(); string userName = User.Identity.GetUserName(); var currentUser = this.Data.GetRepository<ApplicationUser>() .All() .FirstOrDefault(u => u.Id == userId); if (currentUser != null) { var newComment = new Comment { Author = currentUser, Ticket = ticket, Content = comment.Content }; this.Data.GetRepository<Comment>().Add(newComment); this.Data.Save(); var returnComment = new CommentViewModel { Author = userName, Content = comment.Content }; return PartialView("_Comment", returnComment); } else { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } } else { return HttpNotFound(); } } else { return new HttpStatusCodeResult(HttpStatusCode.BadRequest, GetModelStateErrors()); } }
public ActionResult PostComment(CommentDetailsViewModel commentViewModel) { if (!this.ModelState.IsValid) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } var userId = this.User.Identity.GetUserId(); var userName = this.User.Identity.GetUserName(); var ticket = this.Data.Tickets.GetById(commentViewModel.TicketId); if (ticket == null) { return new HttpStatusCodeResult(HttpStatusCode.NotFound); } var comment = new Comment() { UserId = userId, TicketId = commentViewModel.TicketId, Content = commentViewModel.Content }; commentViewModel.UserName = userName; this.Data.Comments.Add(comment); this.Data.SaveChanges(); return this.RedirectToAction("Details", new { Id = comment.TicketId }); }