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")); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } Instructor instructor = await _context.Instructors .Include(i => i.Courses) .SingleAsync(i => i.ID == id); if (instructor == null) { return(RedirectToPage("./Index")); } var departments = await _context.Departments .Where(d => d.InstructorID == id) .ToListAsync(); departments.ForEach(d => d.InstructorID = null); _context.Instructors.Remove(instructor); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int id) { if (!ModelState.IsValid) { return(Page()); } var departmentToUpdate = await new DepartmentMapper().ManyTo(_context.Departments) .FirstOrDefaultAsync(d => d.Id == id); // null means Department was deleted by another user. if (departmentToUpdate == null) { return(HandleDeletedDepartment()); } departmentToUpdate.Name = Department.Name; departmentToUpdate.StartDate = Department.StartDate; departmentToUpdate.Budget = Department.Budget; departmentToUpdate.Administrator.Id = Department.Administrator.Id; _context.Departments.Update(new DepartmentMapper().SingleFrom(departmentToUpdate)); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } var player = await _context.Players.FindAsync(id); if (player == null) { return(NotFound()); } try { _context.Players.Remove(player); 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 (id == null) { return(NotFound()); } var courseToUpdate = await _context.Courses.FindAsync(id); if (courseToUpdate == null) { return(NotFound()); } if (await TryUpdateModelAsync <Course>( courseToUpdate, "course", // Prefix for form value. c => c.Credits, c => c.DepartmentID, c => c.Title)) { await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } // Select DepartmentID if TryUpdateModelAsync fails. PopulateDepartmentsDropDownList(_context, courseToUpdate.DepartmentID); return(Page()); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } Coach coach = await _context.Coaches .Include(i => i.TeamAssignments) .SingleAsync(i => i.ID == id); if (coach == null) { return(RedirectToPage("./Index")); } var departments = await _context.Departments .Where(d => d.CoachID == id) .ToListAsync(); departments.ForEach(d => d.CoachID = null); _context.Coaches.Remove(coach); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
//[BindProperty] //public StudentVM StudentVM { get; set; } public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } var emptyStudent = new Student(); if (await TryUpdateModelAsync <Student>( emptyStudent, "student", s => s.FirstMidName, s => s.LastName, s => s.EnrollmentDate)) { _context.Student.Add(emptyStudent); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } //另一种方法更新需要更新的列值 /* * var entry = _context.Add(new Student()); * entry.CurrentValues.SetValues(StudentVM); * await _context.SaveChangesAsync(); * return RedirectToPage("./Index"); */ return(null); }
public async Task <IActionResult> OnPostAsync(string[] selectedTeams) { var newCoach = new Coach(); if (selectedTeams != null) { newCoach.TeamAssignments = new List <TeamAssignment>(); foreach (var team in selectedTeams) { var teamToAdd = new TeamAssignment { TeamID = int.Parse(team) }; newCoach.TeamAssignments.Add(teamToAdd); } } if (await TryUpdateModelAsync <Coach>( newCoach, "Coach", i => i.FirstMidName, i => i.LastName, i => i.HireDate, i => i.OfficeAssignment)) { _context.Coaches.Add(newCoach); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } PopulateAssignedTeamData(_context, newCoach); return(Page()); }
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) { return(RedirectToAction("./Delete", new { id, saveChangesError = true })); } }
public async Task <IActionResult> OnPostAsync(int?id) { if (!ModelState.IsValid) { return(Page()); } var instructorToUpdate = await _context.Instructors .Include(i => i.OfficeAssignment) .FirstOrDefaultAsync(s => s.ID == id); 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; } try { await _context.SaveChangesAsync(); } catch (DbUpdateException /* ex */) { //Log the error (uncomment ex variable name and write a log.) ModelState.AddModelError("", "Unable to save changes. "); } } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } var student = await _context.Students .AsNoTracking() .FirstOrDefaultAsync(m => m.ID == 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 = id, saveChangesError = true })); } }
public async Task <IActionResult> OnPostAsync(string[] selectedCourses) { var newInstructor = new Instructor(); if (selectedCourses != null) { newInstructor.CourseAssignments = new List <CourseAssignment>(); foreach (var course in selectedCourses) { var courseToAdd = new CourseAssignment { CourseID = int.Parse(course) }; newInstructor.CourseAssignments.Add(courseToAdd); } } if (await TryUpdateModelAsync <Instructor>( newInstructor, "Instructor", i => i.FirstMidName, i => i.LastName, i => i.HireDate, i => i.OfficeAssignment)) { _context.Instructors.Add(newInstructor); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } PopulateAssignedCourseData(_context, newInstructor); return(Page()); }
public async Task <IActionResult> OnPostAsync(int?id, string[] selectedCourses) { if (!ModelState.IsValid) { return(Page()); } var instructorToUpdate = await _context.Instructors .Include(i => i.OfficeAssignment) .Include(i => i.CourseAssignments) .ThenInclude(i => i.Course) .FirstOrDefaultAsync(s => s.ID == id); 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(_context, selectedCourses, instructorToUpdate); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } UpdateInstructorCourses(_context, 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.Departments.Add(Department); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } var entry = _context.Add(new Student()); entry.CurrentValues.SetValues(StudentVM); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int?id) { Instructor instructor = await _context.Instructors .Include(i => i.CourseAssignments) .SingleAsync(i => i.ID == id); var departments = await _context.Departments .Where(d => d.DepartmentID == id) .ToListAsync(); _context.Instructors.Remove(instructor); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int id) { Instructor instructor = await _context.Instructors .SingleAsync(i => i.ID == id); var departments = await _context.Departments .Where(d => d.InstructorID == id) .ToListAsync(); departments.ForEach(d => d.InstructorID = null); _context.Instructors.Remove(instructor); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } var courseToDelete = await new CourseMapper().ManyTo(_context.Courses.AsNoTracking()).FirstOrDefaultAsync(m => m.CourseId == id); if (courseToDelete != null) { _context.Courses.Remove(new CourseMapper().SingleFrom(courseToDelete)); 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, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { var emptyStudent = new Student(); if (await TryUpdateModelAsync <Student>( emptyStudent, "student", // Prefix for form value. s => s.FirstMidName, s => s.LastName, s => s.EnrollmentDate)) { _context.Students.Add(emptyStudent); 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> OnPostAsync(int id) { try { if (await _context.Departments.AnyAsync( m => m.DepartmentID == id)) { _context.Departments.Remove(Department); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); } catch (DbUpdateConcurrencyException) { return(RedirectToPage("./Delete", new { concurrencyError = true, id = id })); } }
public async Task <IActionResult> OnPostAsync(int id) { try { if (await _context.Departments.AnyAsync( m => m.DepartmentID == id)) { // Department.rowVersion 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() { var emptyCourse = new Course(); if (await TryUpdateModelAsync( emptyCourse, "course", c => c.CourseID, c => c.Title, c => c.Credits, c => c.DepartmentID)) { _context.Courses.Add(emptyCourse); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } PopulateDepartmentsDropDownList(_context, emptyCourse.DepartmentID); return(Page()); }
public async Task <IActionResult> OnPostAsync() { var emptyCourse = new Course(); if (await TryUpdateModelAsync <Course>( emptyCourse, "course", // Prefix for form value. s => s.CourseID, s => s.DepartmentID, s => s.Title, s => s.Credits)) { _context.Courses.Add(emptyCourse); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } // Select DepartmentID if TryUpdateModelAsync fails. PopulateDepartmentsDropDownList(_context, emptyCourse.DepartmentID); return(Page()); }
public async Task <IActionResult> OnPostAsync(int?id) { if (!ModelState.IsValid) { return(Page()); } var courseToUpdate = await _context.Courses.FindAsync(id); if (await TryUpdateModelAsync <Course>(courseToUpdate, "course", c => c.Credits, c => c.DepartmentID, c => c.Title)) { await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } PopulateDepartmentsDropDownList(_context, courseToUpdate.DepartmentID); return(Page()); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } Course = await _context.Courses .AsNoTracking() .FirstOrDefaultAsync(m => m.CourseID == id); if (Course != null) { _context.Courses.Remove(Course); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int?id) { if (!ModelState.IsValid) { return(Page()); } var studentToUpdate = await _context.Students.FindAsync(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 emptyCourse = new Course(); if (await TryUpdateModelAsync <Course>(emptyCourse, "course", s => s.CourseID, s => s.DepartmentID, s => s.Title, s => s.Credits)) { _context.Courses.Add(emptyCourse); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } PopulateDepartmentsDropDownList(_context, emptyCourse.DepartmentID); return(Page()); }
public async Task <IActionResult> OnPostAsync(int id) { // 必须包含CourseAssignments,否则删除讲师时将不会删除课程 Instructor instructor = await _context.Instructors .Include(i => i.CourseAssignments) .SingleAsync(i => i.ID == id); // 如果要删除的讲师被指派为任何系的管理员,则需从这些系中去除该讲师的指定 var departments = await _context.Departments .Where(d => d.InstructorID == id) .ToListAsync(); departments.ForEach(d => d.InstructorID = null); _context.Instructors.Remove(instructor); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }