public ActionResult Index(int id, TaskModel model) { using (var dataContext = new DataContext()) { var item = dataContext.Tasks.FirstOrDefault(x => x.Id == model.Id && x.User.UserName == User.Identity.Name); if (item == null) return new HttpStatusCodeResult(HttpStatusCode.NotFound); item.Title = model.Title; item.Completed = model.Completed; dataContext.SaveChanges(); return new HttpStatusCodeResult(HttpStatusCode.OK); } }
// POST /Tasks /// <summary> /// Posts the specified model. /// </summary> /// <param name="model">The model.</param> public void Post(TaskModel model) { using (var dataContext = new DataContext()) { var item = new Task { Title = model.Title, Completed = model.Completed, User = dataContext.UserProfiles.AsNoTracking().First(x => x.UserName == this.User.Identity.Name) }; dataContext.Tasks.Add(item); dataContext.SaveChanges(); } }
public ActionResult Index(TaskModel model) { using (var dataContext = new DataContext()) { var item = new Task { Title = model.Title, Completed = model.Completed, User = dataContext.UserProfiles.AsNoTracking().First(x => x.UserName == User.Identity.Name) }; dataContext.Tasks.Add(item); dataContext.SaveChanges(); return Json(item, JsonRequestBehavior.AllowGet); } }
// PUT api/<controller>/5 /// <summary> /// Puts the specified id. /// </summary> /// <param name="id">The id.</param> /// <param name="model">The model.</param> /// <exception cref="System.Web.Http.HttpResponseException"></exception> public void Put(int id, TaskModel model) { using (var dataContext = new DataContext()) { var item = dataContext.Tasks.FirstOrDefault(x => x.Id == model.Id && x.User.UserName == this.User.Identity.Name); if (item == null) throw new HttpResponseException(HttpStatusCode.NotFound); item.Title = model.Title; item.Completed = model.Completed; dataContext.SaveChanges(); } }