public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } var dog = await _context.Dogs.FindAsync(id); if (dog == null) { return(NotFound()); } try { _context.Dogs.Remove(dog); 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, saveChangesError = true })); } }
// To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync(int id) { var dogToUpdate = await _context.Dogs.FindAsync(id); if (dogToUpdate == null) { return(NotFound()); } if (await TryUpdateModelAsync <Dog>( dogToUpdate, "dog", d => d.Name, d => d.Breed, d => d.Sex, d => d.Summary, d => d.ImageUrl, d => d.Adoptions)) { await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } if (!ModelState.IsValid) { return(Page()); } _context.Attach(Dog).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!DogExists(Dog.ID)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); }
// To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { var emptyDog = new Dog(); if (await TryUpdateModelAsync <Dog>(emptyDog, "dog", d => d.Name, d => d.Breed, d => d.Sex, d => d.Summary, d => d.ImageUrl, d => d.Adoptions)) { if (!ModelState.IsValid) { return(Page()); } } _context.Dogs.Add(emptyDog); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }