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(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync() { 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")); }
public async Task <IActionResult> OnPostAsync(int?id) { if (!ModelState.IsValid) { return(Page()); } var courseToUpdate = await _context.Courses.FindAsync(id); if (Course == 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() { 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(Student); // await _context.SaveChangesAsync(); // return RedirectToPage("./Index"); //} var entry = _context.Add(new Student()); entry.CurrentValues.SetValues(StudentVM); 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 (id == null) { return(NotFound()); } 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) { return(RedirectToPage("./Delete", new { id, saveChangesError = true })); } }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } Course = await _context.Courses.AsNoTracking().SingleAsync(c => c.CourseID == id); if (Course != null) { _context.Courses.Remove(Course); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); }
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 (!ModelState.IsValid) { return(Page()); } var studentToUpdate = await _context.Student.FindAsync(id); if (await TryUpdateModelAsync <Student>(studentToUpdate, "student", s => s.LastName, s => s.FirstMidName, s => s.EnrollmentDate)) { 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.InstructorID == id) .ToListAsync(); departments.ForEach(d => d.InstructorID = null); _context.Instructors.Remove(instructor); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
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); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } 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(string[] selectedCourses) { if (!ModelState.IsValid) { return(Page()); } 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()); }