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)
        {
            //Instructor = await _context.Instructors.ToListAsync();
            InstructorData             = new InstructorIndexData();
            InstructorData.Instructors = await _context.Instructors
                                         .Include(i => i.OfficeAssignment)
                                         .Include(i => i.CourseAssignments)
                                         .ThenInclude(i => i.Course)
                                         .ThenInclude(i => i.Department)

                                         /*
                                          * This was added for eager loading, however, if the user barely need to look at enrolment, we could delegate the query until user selects it
                                          */
                                         //.Include(i => i.CourseAssignments)
                                         //    .ThenInclude(i => i.Course)
                                         //    .ThenInclude(i => i.Enrollments)
                                         //    .ThenInclude(i => i.Student)
                                         //.AsNoTracking()
                                         .OrderBy(i => i.LastName)
                                         .ToListAsync();

            /*
             * The filters will return a collection of a single item
             * .Single() converts that to a single Instructor/Course entity
             * - will complain if the collection is empty though
             * .SingleOrDefault() will return null if the collection is empty
             */

            if (id != null)
            {
                InstructorID = id.Value;

                /*
                 * .Where(cond).Single() or .Single(cond) are equivalent
                 */
                //Instructor instructor = InstructorData.Instructors.Where(i => i.ID == id.Value).Single();
                Instructor instructor = InstructorData.Instructors.Single(i => i.ID == id.Value);

                InstructorData.Courses = instructor.CourseAssignments.Select(s => s.Course);
            }

            if (CourseID != null)
            {
                this.CourseID = CourseID.Value;
                var selectedCourse = InstructorData.Courses
                                     .Where(s => s.CourseID == CourseID.Value)
                                     .Single();

                /*
                 * Added this to only load enrolment details when clicked
                 */
                await _context.Entry(selectedCourse).Collection(x => x.Enrollments).LoadAsync();

                foreach (Enrollment en in selectedCourse.Enrollments)
                {
                    await _context.Entry(en).Reference(x => x.Student).LoadAsync();
                }

                InstructorData.Enrollments = selectedCourse.Enrollments;
            }
        }