public async Task <IActionResult> PutProduct(int id, Product product) { if (id != product.Id) { return(BadRequest()); } _context.Entry(product).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutTodoItem(long id, TodoItem todoItem) { if (id != todoItem.Id) { return(BadRequest()); } if (!TryValidateModel(todoItem, nameof(todoItem)))//修改后重新验证 { return(BadRequest(JsonSerializer.Serialize(ModelState.Values.Where(x => x.Errors.Count > 0).Select(x => new { key = x.GetKeyValue("Key"), x.RawValue, x.Errors.First().ErrorMessage }).ToList()))); } _context.Entry(todoItem).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TodoItemExists(id)) { return(NotFound()); } else { throw;//new Exception(""); } } return(NoContent()); }