public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { } var team = await _context.Team .AsNoTracking() .FirstOrDefaultAsync(m => m.ID == id); if (Team == null) { return(NotFound()); } try { _context.Team.Remove(team); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } catch (DbUpdateException /* ex */) { //Log the error (uncomment ex variable name and write a log.) // If Remove() fails, OnGetAsync is called with savesChangesError=true return(RedirectToAction("./Delete", new { id, saveChangesError = true })); } }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } var emptyTeam = new Team(); if (await TryUpdateModelAsync <Team>( emptyTeam, "team", // Prefix for form value t => t.TeamName, t => t.TeamLocation)) { _context.Team.Add(emptyTeam); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } return(null); }
// Called when the user submits data to the page at /Team/Edit/<id> public async Task <IActionResult> OnPostAsync(int?id) { if (!ModelState.IsValid) { return(Page()); } // Chose FindAsync over FirstOrDefaultAsync // Better choice when selecting an entity from a primary key var teamToUpdate = await _context.Team.FindAsync(id); // Attempts to update Team with the listed properties if (await TryUpdateModelAsync <Team>( teamToUpdate, "team", // Prefix for form value. t => t.TeamName, t => t.TeamLocation)) { await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } return(Page()); }