//Important OnPost"Delete"Async is the name of the handler. which is how asp-page-handler can access this //this looks for the Customer object in the db with the id //if found removes and saves the chanages then reloads the page public async Task <IActionResult> OnPostDeleteAsync(int id) { var cx = await _db.Customers.FindAsync(id); if (cx != null) { _db.Customers.Remove(cx); await _db.SaveChangesAsync(); } return(RedirectToPage()); }
//update the Customer object public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _db.Attach(Cx).State = EntityState.Modified; try { await _db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException e) { throw new Exception($"Cx : {Cx.Id} not found", e); } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { //if the state of the model is in valid return this page? return(Page()); } // add the customer from the form (cx) into the list in the in memory db _db.Customers.Add(Cx); await _db.SaveChangesAsync(); //set the message string this can get caught by index Message = $"Cx {Cx.Name} added!"; //sending the message to the logg _log.LogCritical(Message.ToString()); //go to index when done return(RedirectToPage("/Index")); }