コード例 #1
0
        // GET: Instructors
        public async Task<ActionResult> Index(int? id, int? courseID)
        {
            //var instructors = db.Instructors.Include(i => i.OfficeAssignment);
            //return View(await instructors.ToListAsync());

            var viewModel = new InstructorIndexData();
            viewModel.Instructors = db.Instructors
                .Include(i => i.OfficeAssignment)
                .Include(i => i.Courses.Select(c => c.Department))
                .OrderBy(i => i.LastName);

            if (id != null)
            {
                ViewBag.InstructorID = id.Value;
                viewModel.Courses = viewModel.Instructors.Where(i => 
                    i.InstructorID == id.Value).Single().Courses;
            }

            if (courseID != null)
            {
                ViewBag.CourseID = courseID.Value;
                viewModel.Enrollments = viewModel.Courses.Where(
                    x => x.CourseID == courseID).Single().Enrollments;
            }

            return View(viewModel);
        }
コード例 #2
0
        // GET: Instructor
        public ActionResult Index(int? id, int? courseID)
        {
            var viewModel = new InstructorIndexData();
            viewModel.Instructors = db.Instructors
                .Include(i => i.OfficeAssignment)
                .Include(i => i.Courses.Select(c => c.Department))
                .OrderBy(i => i.LastName);

            if (id != null)
            {
                ViewBag.InstructorID = id.Value;
                viewModel.Courses = viewModel.Instructors
                    .Where(i => i.ID == id.Value)
                    .Single()
                    .Courses;
            }

            if (courseID != null)
            {
                ViewBag.CourseID = courseID.Value;
                //Lazy loading
                //viewModel.Enrollments = viewModel.Courses.Where(x => x.CourseID == courseID).Single().Enrollments;
                //Explicit loading
                var selectedCourse = viewModel.Courses.Where(x => x.CourseID == courseID).Single();
                db.Entry(selectedCourse).Collection(x => x.Enrollments).Load();
                foreach (Enrollment enrollment in selectedCourse.Enrollments)
                {
                    db.Entry(enrollment).Reference(x => x.Student).Load();
                }

                viewModel.Enrollments = selectedCourse.Enrollments;
            }

            return View(viewModel);
        }
コード例 #3
0
        // GET: Instructor
        public ActionResult Index(int? id, int? courseID)
        {
            var viewModel = new InstructorIndexData();

            viewModel.Instructors = db.Instructors
                .Include(i => i.OfficeAssignment)
                .Include(i => i.Courses.Select(c => c.Department))
                .OrderBy(i => i.LastName);

            if (id != null)
            {
                ViewBag.InstructorID = id.Value;
                viewModel.Courses = viewModel.Instructors
                    .Single(i => i.ID == id.Value).Courses;
            }

            if (courseID != null)
            {
                ViewBag.CourseID = courseID.Value;
                viewModel.Enrollments = viewModel.Courses
                    .Single(x => x.CourseID == courseID).Enrollments;
            }

            var instructors = db.Instructors.Include(i => i.OfficeAssignment);
            return View(viewModel);
        }
コード例 #4
0
        //
        // GET: /Instructor/

        public ActionResult Index(int? id, int? courseID)
        {
            var viewModel = new InstructorIndexData();
            viewModel.Instructors = instructorRepository.GetInstructorsWithOfficeAssigmentAndCourses()
                .OrderBy(i => i.LastName);

            if (id != null)
            {
                ViewBag.InstructorID = id.Value;
                viewModel.Courses = viewModel.Instructors.
                    Where(i => i.InstructorID == id.Value).Single().Courses;
            }

            if (courseID != null)
            {
                ViewBag.CourseID = courseID.Value;
                viewModel.Enrollments = viewModel.Courses
                    .Where(c => c.CourseID == courseID.Value).Single().Enrollments;
            }

            return View(viewModel);
        }
コード例 #5
0
        //
        // GET: /Instructor/
        public ActionResult Index(Int32? id, Int32? courseID)
        {
            var viewModel = new InstructorIndexData();
            viewModel.Instructors = db.Instructors
                .Include(i => i.Courses.Select(c => c.Department))
                .OrderBy(i => i.LastName);

            if (id != null)
            {
                ViewBag.PersonID = id.Value;
                viewModel.Courses = viewModel.Instructors.Where(i => i.PersonID == id.Value).Single().Courses;
            }

            //if (courseID != null)
            //{
            //    ViewBag.CourseID = courseID.Value;
            //    viewModel.Enrollments = viewModel.Courses.Where(x => x.CourseID == courseID).Single().Enrollments;
            //}

            if (courseID != null)
            {
                ViewBag.CourseID = courseID.Value;

                var selectedCourse = viewModel.Courses.Where(x => x.CourseID == courseID).Single();
                db.Entry(selectedCourse).Collection(x => x.Enrollments).Load();
                foreach (Enrollment enrollment in selectedCourse.Enrollments)
                {
                    db.Entry(enrollment).Reference(x => x.Student).Load();
                }

                viewModel.Enrollments = selectedCourse.Enrollments;
            }

            return View(viewModel);
        }
コード例 #6
0
        //
        // GET: /Instructor/
        public ActionResult Index(int? id, int? courseID)
        {
            var viewModel = new InstructorIndexData();
            viewModel.Instructors = db.Instructors
                .Include(i => i.OfficeAssignment)
                .Include(i => i.Courses.Select(c => c.Department)) // loads Courses, and for each Course that is loaded it does eager loading for the Course.Department navigation property.
                .OrderBy(i => i.LastName);
            /*
             * When you retrieved the list of instructors, you specified eager loading for the Courses navigation property and for the Department
             * property of each course. Then you put the Courses collection in the view model, and now you're accessing the Enrollments navigation
             * property from one entity in that collection. Because you didn't specify eager loading for the Course.Enrollments navigation property,
             * the data from that property is appearing in the page as a result of lazy loading.
             */

            if (id != null)
            {
                /*
                 If an instructor ID was selected, the selected instructor is retrieved from the list of instructors in the view model.
                 * The view model's Courses property is then loaded with the Course entities from that instructor's Courses navigation property.
                 *
                 */
                ViewBag.PersonID = id.Value;
                viewModel.Courses = viewModel.Instructors.Where(
                    i => i.PersonID == id.Value).Single().Courses;
                /*
                 You use the Single method on a collection when you know the collection will have only one item. The Single method throws
                 * an exception if the collection passed to it is empty or if there's more than one item. An alternative is SingleOrDefault,
                 * which returns a default value (null in this case) if the collection is empty. However, in this case that would still result
                 * in an exception (from trying to find a Courses property on a null reference), and the exception message would less clearly
                 * indicate the cause of the problem.
                 */
            }

            if (courseID != null)
            {
                ViewBag.CourseID = courseID.Value;
                // lazy loading
                //viewModel.Enrollments = viewModel.Courses.Where(
                //    x => x.CourseID == courseID).Single().Enrollments;

                // example of explicit loading of the Enrollments property.
                var selectedCourse = viewModel.Courses.Where(x => x.CourseID == courseID).Single();
                db.Entry(selectedCourse).Collection(x => x.Enrollments).Load(); //  explicitly loads that course's Enrollments navigation property:
                foreach (Enrollment enrollment in selectedCourse.Enrollments)
                {
                    db.Entry(enrollment).Reference(x => x.Student).Load(); // explicitly loads each Enrollment entity's related Student entity:
                }
                /* Notice that you use the Collection method to load a collection property, but for a property that holds just one entity, you use the Reference method. */

                viewModel.Enrollments = selectedCourse.Enrollments;
            }

            return View(viewModel);
        }
コード例 #7
0
        //
        // GET: /Instructor/
        /// <summary>
        /// The method accepts optional query string parameters that provide the ID values of the selected instructor 
        /// and selected course, and passes all of the required data to the view. The query string parameters are 
        /// provided by the Select hyperlinks on the page.
        /// </summary>
        public ActionResult Index(Int32? id, Int32? courseID)
        {
            var viewModel = new InstructorIndexData();

            viewModel.Instructors = db.Instructors
                .Include(i => i.Courses.Select(c => c.Department))
                .OrderBy(i => i.LastName);

            if (id != null)
            {
                ViewBag.PersonID = id.Value;
                viewModel.Courses = viewModel.Instructors.Where(i => i.PersonID == id.Value).Single().Courses;
            }

            if (courseID != null)
            {
                ViewBag.CourseID = courseID.Value;

                // explicit loading
                var selectedCourse = viewModel.Courses.Where(x => x.CourseID == courseID).Single();
                db.Entry(selectedCourse).Collection(x => x.Enrollments).Load();

                foreach (Enrollment enrollment in selectedCourse.Enrollments)
                {
                    // normally use the Collection method to load a collection property, but
                    // for a property that holds just one entity, you use the Reference method
                    db.Entry(enrollment).Reference(x => x.Student).Load();
                }

                viewModel.Enrollments = selectedCourse.Enrollments;
            }

            return View(viewModel);
        }