public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Product.Add(Product); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int id) { if (!ModelState.IsValid) { return(Page()); } var productToUpdate = await _context.Product .Include(p => p.ID) .FirstOrDefaultAsync(m => m.ID == id); if (productToUpdate == null) { return(HandleDeletedProduct()); } _context.Entry(productToUpdate).Property("RowVersion").OriginalValue = Product.RowVersion; if (await TryUpdateModelAsync <Product>( productToUpdate, "Produkter", s => s.Titel, s => s.Description, s => s.Price, s => s.ImageURL)) { try { await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } catch (DbUpdateConcurrencyException ex) { var exceptionEntry = ex.Entries.Single(); var clientValues = (Product)exceptionEntry.Entity; var databaseEntry = exceptionEntry.GetDatabaseValues(); if (databaseEntry == null) { ModelState.AddModelError(string.Empty, "Unable to save. " + "The product was deleted by another user."); return(Page()); } var dbValues = (Product)databaseEntry.ToObject(); SetDbErrorMessage(dbValues, clientValues, _context); Product.RowVersion = (byte[])dbValues.RowVersion; ModelState.Remove("Product.RowVersion"); } } return(Page()); }
public async Task <IActionResult> OnPostAsync(int id) { try { if (await _context.Product.AnyAsync( m => m.ID == id)) { _context.Product.Remove(Product); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); } catch (DbUpdateConcurrencyException) { return(RedirectToPage("./Delete", new { concurrencyError = true, id = id })); } }