public JsonResult CreateTodo([DataSourceRequest] DataSourceRequest request, TodoViewModel todo) { if (todo != null && ModelState.IsValid) { Plan currentPlan = this.db.Plans.All().FirstOrDefault(p=>p.Title == todo.PlanName); if (currentPlan != null) { Todo newTodo = new Todo() { Title = todo.Title, Description = todo.Description, DateCreated = DateTime.Now, Plan = currentPlan, State = todo.State, Priority = todo.Priority }; this.db.Todos.Add(newTodo); this.db.SaveChanges(); todo.Id = newTodo.Id; todo.PlanName = currentPlan.Title; } } return Json(new[] { todo }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet); }
public JsonResult DeleteTodo([DataSourceRequest] DataSourceRequest request, TodoViewModel todo) { var existingTodo = this.db.Todos.Find(todo.Id); if (existingTodo != null && ModelState.IsValid) { this.db.Todos.Delete(existingTodo); this.db.SaveChanges(); } return Json(new[] {todo}, JsonRequestBehavior.AllowGet); }
public JsonResult UpdateTodo([DataSourceRequest] DataSourceRequest request, TodoViewModel todo) { var existingTodo = this.db.Todos.Find(todo.Id); var existingPlan = this.db.Plans.All().FirstOrDefault(p => p.Title == todo.PlanName); if (existingTodo != null && existingPlan!=null && ModelState.IsValid) { existingTodo.Title = todo.Title; existingTodo.Description = todo.Description; existingTodo.DateCreated = DateTime.Now; existingTodo.Plan = existingPlan; existingTodo.State = todo.State; existingTodo.Priority = todo.Priority; this.db.SaveChanges(); todo.Id = existingTodo.Id; todo.PlanName = todo.PlanName; } return Json(new[] {todo}.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet); }