public async Task <IHttpActionResult> Update(int id, [FromBody] TodoUpdate todoUpdate) { if (todoUpdate == null || ModelState.IsValid == false) { return(BadRequest()); } var foundTodo = await _todoDbContext.Todoes.SingleOrDefaultAsync(todo => todo.Id == id); if (foundTodo == null) { return(NotFound()); } _todoDbContext.Entry(foundTodo).CurrentValues.SetValues(todoUpdate); await _todoDbContext.SaveChangesAsync(); return(ResponseMessage(new HttpResponseMessage(HttpStatusCode.NoContent))); }
public IActionResult Put(int id, [FromBody] TodoUpdate model) { if (model == null || string.IsNullOrEmpty(model.Name)) { return(BadRequest()); } var todo = TodoList.FirstOrDefault(t => t.Id == id); if (todo == null) { return(NotFound()); } todo.Name = model.Name; todo.Completed = model.Completed; return(Ok(todo)); }
public async Task <ActionResult> Edit([Bind(Include = "Id,Description,IsCompleted")] Todo todo) { if (!ModelState.IsValid) { View(todo); } TodoUpdate todoUpdate = new TodoUpdate { IsCompleted = todo.IsCompleted, Description = todo.Description }; var todoUpdateSerialized = JsonConvert.SerializeObject(todoUpdate); HttpContent content = new StringContent(todoUpdateSerialized, Encoding.UTF8, MimeTypeApplicationJson); await _httpClient.SendAsync( new HttpRequestMessage(new HttpMethod("PATCH"), $"{TodoResourceName}/{todo.Id}") { Content = content }); return(RedirectToAction("Index")); }