public async Task OnGetAsync(int?id, int?courseID) { Instructor = new InstructorIndexData(); Instructor.Instructors = await _context.Instructors .Include(i => i.OfficeAssignment) .Include(i => i.CourseAssignments) .ThenInclude(i => i.Course) .ThenInclude(i => i.Department) //.Include(i => i.CourseAssignments) // .ThenInclude(i => i.Course) // .ThenInclude(i => i.Enrollments) // .ThenInclude(i => i.Student) // .AsNoTracking() .OrderBy(i => i.LastName) .ToListAsync(); if (id != null) { InstructorID = id.Value; Instructor instructor = Instructor.Instructors.Where( i => i.ID == id.Value).Single(); Instructor.Courses = instructor.CourseAssignments.Select(s => s.Course); } if (courseID != null) { CourseID = courseID.Value; var selectedCourse = Instructor.Courses.Where(x => x.CourseID == courseID).Single(); await _context.Entry(selectedCourse).Collection(x => x.Enrollments).LoadAsync(); foreach (Enrollment enrollment in selectedCourse.Enrollments) { await _context.Entry(enrollment).Reference(x => x.Student).LoadAsync(); } Instructor.Enrollments = selectedCourse.Enrollments; } }
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); // null means Department was deleted by another user. if (departmentToUpdate == null) { return(await HandleDeletedDepartment()); } // Update the RowVersion to the value when this entity was // fetched. If the entity has been updated after it was // fetched, RowVersion won't match the DB RowVersion and // a DbUpdateConcurrencyException is thrown. // A second postback will make them match, unless a new // concurrency issue happens. _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; // Must clear the model error for the next postback. ModelState.Remove("Department.RowVersion"); } } InstructorNameSL = new SelectList(_context.Instructors, "ID", "FullName", departmentToUpdate.InstructorID); return(Page()); }