public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Attach(Rental).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!RentalExists(Rental.RentalID)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Rentals.Add(Rental); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } var rental = await _context.Rentals .AsNoTracking() .FirstOrDefaultAsync(m => m.RentalID == id); if (rental == null) { return(NotFound()); } try { _context.Rentals.Remove(rental); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } catch (DbUpdateException /* ex */) { //Log the error (uncomment ex variable name and write a log.) return(RedirectToAction("./Delete", new { id = id, saveChangesError = true })); } /* Old, changing to above per tutorial. * Rental = await _context.Rentals.FindAsync(id); * * if (Rental != null) * { * _context.Rentals.Remove(Rental); * await _context.SaveChangesAsync(); * } * * return RedirectToPage("./Index");*/ }
public async Task <IActionResult> OnPostAsync() { // Perform an initial check to catch FileUpload class // attribute violations. if (!ModelState.IsValid) { File = await _context.File.AsNoTracking().ToListAsync(); return(Page()); } var publicFileData = await FileHelpers.ProcessFormFile(FileUpload.UploadPublicFile, ModelState); var privateFileData = await FileHelpers.ProcessFormFile(FileUpload.UploadPrivateFile, ModelState); // Perform a second check to catch ProcessFormFile method // violations. if (!ModelState.IsValid) { File = await _context.File.AsNoTracking().ToListAsync(); return(Page()); } var file = new File() { PublicFile = publicFileData, PublicFileSize = FileUpload.UploadPublicFile.Length, PrivateFile = privateFileData, PrivateFileSize = FileUpload.UploadPrivateFile.Length, Title = FileUpload.Title, UploadDT = DateTime.Now }; _context.File.Add(file); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } Product = await _context.Products .AsNoTracking() // Added. .FirstOrDefaultAsync(m => m.ProductID == id); // Changed, was .FindAsync(id);. if (Product == null) { return(NotFound()); } try { _context.Products.Remove(Product); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } catch (DbUpdateException /* ex */) { //Log the error (uncomment ex variable name and write a log.) return(RedirectToAction("./Delete", new { id = id, saveChangesError = true })); } /*if (Product != null) Tutorial said to take out. * { * _context.Products.Remove(Product); * await _context.SaveChangesAsync(); * } * * return RedirectToPage("./Index");*/ }