Ejemplo n.º 1
0
        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());
        }
Ejemplo n.º 2
0
        public async Task OnGetAsync(int?id, int?courseID)
        {
            InstructorData             = new InstructorIndexData();
            InstructorData.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 = InstructorData.Instructors
                                        .Where(i => i.ID == id.Value).Single();
                InstructorData.Courses = instructor.CourseAssignments.Select(s => s.Course);
            }

            if (courseID != null)
            {
                CourseID = courseID.Value;
                var selectedCourse = InstructorData.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();
                }
                InstructorData.Enrollments = selectedCourse.Enrollments;
            }
        }