//public async Task<IActionResult> OnPostAsync(int? id) //{ // if (id == null) // { // return NotFound(); // } // Student = await _context.Student.FindAsync(id); // if (Student != null) // { // _context.Student.Remove(Student); // await _context.SaveChangesAsync(); // } // return RedirectToPage("./Index"); //} public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } var student = await _context.Student .AsNoTracking() .FirstOrDefaultAsync(m => m.ID == id); if (student == null) { return(NotFound()); } try { _context.Student.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 })); } }
public async Task <IActionResult> OnPostAsync(int?id) { if (!ModelState.IsValid) { return(Page()); } var studentToUpdate = await _context.Student.FindAsync(id); //_context.Attach(Student).State = EntityState.Modified; // try // { // await _context.SaveChangesAsync(); // } // catch (DbUpdateConcurrencyException) // { // if (!StudentExists(Student.ID)) // { // return NotFound(); // } // else // { // throw; // } // } // return RedirectToPage("./Index"); //} //private bool StudentExists(int id) //{ // return _context.Student.Any(e => e.ID == id); //} if (await TryUpdateModelAsync <Student>( studentToUpdate, "student", s => s.FirstMidName, s => s.LastName, s => s.EnrollmentDate)) { await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } return(Page()); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } var emptyStudent = new Student(); if (await TryUpdateModelAsync <Student>( emptyStudent, "student", // Prefix for form value. s => s.FirstMidName, s => s.LastName, s => s.EnrollmentDate)) { _context.Student.Add(emptyStudent); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } return(null); }