public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } Instructor instructorToDelete = await _context.Instructors .Include(i => i.CourseAssignments) // need this otherwise the course assignments will not be deleted after instructor is .SingleAsync(m => m.ID == id); if (instructorToDelete == null) { return(RedirectToPage("./Index")); } var departments = await _context.Departments .Where(d => d.InstructorID == id) .ToListAsync(); departments.ForEach(d => d.InstructorID = null); _context.Instructors.Remove(instructorToDelete); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public async Task <IActionResult> 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 */) { return(RedirectToAction("./Delete", new { id, saveChangesError = true })); } //if (Student != null) //{ // _context.Students.Remove(Student); // await _context.SaveChangesAsync(); //} // //return RedirectToPage("./Index"); }
public async Task <IActionResult> OnPostAsync(int id) { if (!ModelState.IsValid) { return(Page()); } var departmentToUpdate = await _context.Departments .Include(i => i.Administrator) .FirstOrDefaultAsync(m => m.DepartmentID == id); if (departmentToUpdate == null) { return(HandleDeletedDepartment()); } _context.Entry(departmentToUpdate) .Property("RowVersion").OriginalValue = Department.RowVersion; 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 RowVersion so next postback // matches unless an new concurrency issue happens. Department.RowVersion = (byte[])dbValues.RowVersion; // Clear the model error for the next postback. ModelState.Remove("Department.RowVersion"); } } InstructorNameSL = new SelectList(_context.Instructors, "ID", "FullName", departmentToUpdate.InstructorID); return(Page()); }
// To protect from overposting attacks, please 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.Departments.Add(Department); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
// To protect from overposting attacks, please 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 (id == null) { return(NotFound()); } var courseToUpdate = await _context.Courses.FindAsync(id); if (courseToUpdate == null) { return(NotFound()); } if (await TryUpdateModelAsync <Course>( courseToUpdate, "course", c => c.DepartmentID, c => c.Credits, c => c.Title)) { await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } // Select DepartmentID if update fails PopulateDepartmentsDropDownList(_context, courseToUpdate.DepartmentID); return(Page()); //if (!ModelState.IsValid) //{ // return Page(); //} //_context.Attach(Course).State = EntityState.Modified; //try //{ // await _context.SaveChangesAsync(); //} //catch (DbUpdateConcurrencyException) //{ // if (!CourseExists(Course.CourseID)) // { // return NotFound(); // } // else // { // throw; // } //} }
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, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync(int id) { 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()); //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"); }
// To protect from overposting attacks, please 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(); //} var newCourse = new Course(); if (await TryUpdateModelAsync <Course>( newCourse, "course", s => s.CourseID, s => s.DepartmentID, s => s.Title, s => s.Credits)) { _context.Courses.Add(newCourse); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } // Select DepartmentID if TryUpdateModelAsync fails PopulateDepartmentsDropDownList(_context, newCourse.CourseID); return(Page()); }