public ActionResult AddComment(WorkOrderViewModel model) { if (ModelState.IsValid) { var newComment = new WorkOrderComment() { Comment = model.NewComment, CommentorId = User.Identity.GetUserId(), CreateDateTime = DateTime.Now, WorkOrderId = model.WorkOrder.Id }; _context.WorkOrderComments.Add(newComment); var workOrderInDb = _context.WorkOrders.Single(w => w.Id == model.WorkOrder.Id); workOrderInDb.ModifiedDateTime = DateTime.Now; _context.SaveChanges(); return(RedirectToAction("Details", new { id = model.WorkOrder.Id })); } ModelState.AddModelError(string.Empty, "Comment field is required."); return(RedirectToAction("Details", new { id = model.WorkOrder.Id })); }
public ActionResult Create(CreateWorkOrderViewModel model) { var userId = User.Identity.GetUserId(); var appUser = _context.AppUsers.Single(a => a.Id == userId); if (User.IsInRole(RoleName.LeaseHolder)) { var leaseHolder = (LeaseHolder)appUser; model.RequestorId = User.Identity.GetUserId(); model.AssignedUserId = null; model.ExpectedCompletionDateTime = null; model.LocationId = leaseHolder.LocationId; model.UnitId = leaseHolder.UnitId; model.StatusId = WorkOrderStatus.New; } if (!ModelState.IsValid) { model.Categories = _context.WorkOrderCategories.OrderBy(w => w.Name).ToList(); model.Locations = _context.Locations.OrderBy(l => l.Name).ToList(); model.Statues = _context.WorkOrderStatus.ToList(); model.Employees = _context.AppUsers.Where(a => !(a is LeaseHolder)).ToList(); model.Users = _context.AppUsers.ToList(); return(View(model)); } var newWorkOrder = new WorkOrder() { AssignedUserId = model.AssignedUserId, CategoryId = model.CategoryId, CreateDateTime = DateTime.Now, ExpectedCompletionDateTime = model.ExpectedCompletionDateTime, LocationId = model.LocationId, ModifiedDateTime = DateTime.Now, RequestorId = model.RequestorId, StatusDateTime = DateTime.Now, StatusId = model.StatusId, Subject = model.Subject, UnitId = model.UnitId }; _context.WorkOrders.Add(newWorkOrder); _context.SaveChanges(); var newWorkOrderComment = new WorkOrderComment() { Comment = model.WorkOrderDescription, CommentorId = User.Identity.GetUserId(), CreateDateTime = DateTime.Now, WorkOrderId = newWorkOrder.Id }; _context.WorkOrderComments.Add(newWorkOrderComment); _context.SaveChanges(); newWorkOrder = _context.WorkOrders .Include(w => w.AssignedUser) .Include(w => w.Category) .Include(w => w.Location) .Include(w => w.Requestor) .Include(w => w.Status) .Include(w => w.Unit) .Single(w => w.Id == newWorkOrder.Id); SendEmail("new", newWorkOrder); return(RedirectToAction("Index", "WorkOrder")); }