public async Task <IActionResult> Edit(long id, [Bind("Id,Name")] Category category) { if (id != category.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(category); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CategoryExists(category.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(category)); }
public async Task <IActionResult> Edit(int id, [Bind("Id,Nome")] Departamento departamento) { if (id != departamento.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(departamento); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!DepartamentoExists(departamento.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(departamento)); }
public async Task <IActionResult> Edit(long id, [Bind("Id,Name,Description,Price,CategoryId")] Product product) { if (id != product.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(product); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(product.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Name", product.CategoryId); return(View(product)); }
public async Task UpdateAsync(Seller obj) { bool hasAny = await _context.Seller.AnyAsync(x => x.Id == obj.Id); if (!hasAny) { throw new NotFoundException("Id not found"); } try { _context.Update(obj); await _context.SaveChangesAsync(); } catch (DbConcurrencyException e) { throw new DbConcurrencyException(e.Message); } }
public async Task UpdateAsync(Seller seller) { if (!await Context.Seller.AnyAsync(x => x.Id == seller.Id)) { throw new NotFoundException("Seller Id not found."); } try { Context.Update(seller); await Context.SaveChangesAsync(); } //Layer Segregation //This is important because our service layer cannot propagate an exception related to data access. //If an exception occurs, our service layer is going to throw an exception from its own layer //and our controller needs to deal only with exceptions related to the service layer. //This is a way that we can respect the MVC architecture. catch (DbUpdateConcurrencyException exception) { throw new DbConcurrencyException(exception.Message); } }