using ContosoUniversity.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.EntityFrameworkCore; using System.Threading.Tasks; namespace ContosoUniversity.Pages.Students { #region snippet_All public class DeleteModel : PageModel { private readonly ContosoUniversity.Data.SchoolContext _context; public DeleteModel(ContosoUniversity.Data.SchoolContext context) { _context = context; } [BindProperty] public Student Student { get; set; } public string ErrorMessage { get; set; } public async Task OnGetAsync(int? id, bool? saveChangesError = false) { if (id == null) { return NotFound(); } Student = await _context.Students .AsNoTracking() .FirstOrDefaultAsync(m => m.ID == id); if (Student == null) { return NotFound(); } if (saveChangesError.GetValueOrDefault()) { ErrorMessage = "Delete failed. Try again"; } return Page(); } public async Task OnPostAsync(int? id) { if (id == null) { return NotFound(); } var student = await _context.Students.FindAsync(id); if (student == null) { return NotFound(); } try { _context.Students.Remove(student); 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 }); } } } #endregion }