public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } 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) { _logger.LogError(ex, ErrorMessage); return(RedirectToAction("./Delete", new { id, saveChangesError = true })); } }
// To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Departments.Add(Department); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } Course = await _context.Courses.FindAsync(id); if (Course != null) { _context.Courses.Remove(Course); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); }
// To protect from overposting attacks, enable the specific properties you want to bind to. // For more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync(int id) { //if (!ModelState.IsValid) //{ // return Page(); //} //_context.Attach(Student).State = EntityState.Modified; //try //{ // await _context.SaveChangesAsync(); //} //catch (DbUpdateConcurrencyException) //{ // if (!StudentExists(Student.ID)) // { // return NotFound(); // } // else // { // throw; // } //} //return RedirectToPage("./Index"); var studentToUpdate = await _context.Students.FindAsync(id); if (studentToUpdate == null) { return(NotFound()); } 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(int?id) { if (id == null) { return(NotFound()); } Instructor = await _context.Instructors.FindAsync(id); if (Instructor != null) { _context.Instructors.Remove(Instructor); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); }
//public async Task<IActionResult> OnGetAsync(int? id) //{ // if (id == null) // { // return NotFound(); // } // Department = await _context.Departments // .Include(d => d.Administrator).FirstOrDefaultAsync(m => m.DepartmentID == id); // if (Department == null) // { // return NotFound(); // } // return Page(); //} //public async Task<IActionResult> OnPostAsync(int? id) //{ // if (id == null) // { // return NotFound(); // } // Department = await _context.Departments.FindAsync(id); // if (Department != null) // { // _context.Departments.Remove(Department); // await _context.SaveChangesAsync(); // } // return RedirectToPage("./Index"); //} public async Task <IActionResult> OnPostAsync(int id) { try { if (await _context.Departments.AnyAsync( m => m.DepartmentID == id)) { // Department.ConcurrencyToken value is from when the entity // was fetched. If it doesn't match the DB, a // DbUpdateConcurrencyException exception is thrown. _context.Departments.Remove(Department); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); } catch (DbUpdateConcurrencyException) { return(RedirectToPage("./Delete", new { concurrencyError = true, id = id })); } }
//// To protect from overposting attacks, enable the specific properties you want to bind to. //// For more details, see https://aka.ms/RazorPagesCRUD. //public async Task<IActionResult> OnPostAsync() //{ // if (!ModelState.IsValid) // { // return Page(); // } // _context.Attach(Instructor).State = EntityState.Modified; // try // { // await _context.SaveChangesAsync(); // } // catch (DbUpdateConcurrencyException) // { // if (!InstructorExists(Instructor.ID)) // { // return NotFound(); // } // else // { // throw; // } // } // return RedirectToPage("./Index"); //} //private bool InstructorExists(int id) //{ // return _context.Instructors.Any(e => e.ID == id); //} public async Task <IActionResult> OnPostAsync(int?id, string[] selectedCourses) { if (id == null) { return(NotFound()); } var instructorToUpdate = await _context.Instructors .Include(i => i.OfficeAssignment) .Include(i => i.Courses) .FirstOrDefaultAsync(s => s.ID == id); if (instructorToUpdate == null) { return(NotFound()); } if (await TryUpdateModelAsync <Instructor>( instructorToUpdate, "Instructor", i => i.FirstMidName, i => i.LastName, i => i.HireDate, i => i.OfficeAssignment)) { if (String.IsNullOrWhiteSpace( instructorToUpdate.OfficeAssignment?.Location)) { instructorToUpdate.OfficeAssignment = null; } UpdateInstructorCourses(selectedCourses, instructorToUpdate); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } UpdateInstructorCourses(selectedCourses, instructorToUpdate); PopulateAssignedCourseData(_context, instructorToUpdate); return(Page()); }
// To protect from overposting attacks, enable the specific properties you want to bind to. // For more details, see https://aka.ms/RazorPagesCRUD. //public async Task<IActionResult> OnPostAsync() //{ // if (!ModelState.IsValid) // { // return Page(); // } // _context.Attach(Department).State = EntityState.Modified; // try // { // await _context.SaveChangesAsync(); // } // catch (DbUpdateConcurrencyException) // { // if (!DepartmentExists(Department.DepartmentID)) // { // return NotFound(); // } // else // { // throw; // } // } // return RedirectToPage("./Index"); //} public async Task <IActionResult> OnPostAsync(int id) { if (!ModelState.IsValid) { return(Page()); } // Fetch current department from DB. // ConcurrencyToken may have changed. var departmentToUpdate = await _context.Departments .Include(i => i.Administrator) .FirstOrDefaultAsync(m => m.DepartmentID == id); if (departmentToUpdate == null) { return(HandleDeletedDepartment()); } // Set ConcurrencyToken to value read in OnGetAsync _context.Entry(departmentToUpdate).Property( d => d.ConcurrencyToken).OriginalValue = Department.ConcurrencyToken; if (await TryUpdateModelAsync <Department>( departmentToUpdate, "Department", s => s.Name, s => s.StartDate, s => s.Budget, s => s.InstructorID)) { try { await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } catch (DbUpdateConcurrencyException ex) { var exceptionEntry = ex.Entries.Single(); var clientValues = (Department)exceptionEntry.Entity; var databaseEntry = exceptionEntry.GetDatabaseValues(); if (databaseEntry == null) { ModelState.AddModelError(string.Empty, "Unable to save. " + "The department was deleted by another user."); return(Page()); } var dbValues = (Department)databaseEntry.ToObject(); await SetDbErrorMessage(dbValues, clientValues, _context); // Save the current ConcurrencyToken so next postback // matches unless an new concurrency issue happens. Department.ConcurrencyToken = (byte[])dbValues.ConcurrencyToken; // Clear the model error for the next postback. ModelState.Remove($"{nameof(Department)}.{nameof(Department.ConcurrencyToken)}"); } } InstructorNameSL = new SelectList(_context.Instructors, "ID", "FullName", departmentToUpdate.InstructorID); return(Page()); }