Beispiel #1
0
        public async Task <IActionResult> OnPostAsync(string[] selectedCourses)
        {
            var newInstructor = new InstructorWithCollection();

            if (selectedCourses != null)
            {
                newInstructor.Courses = new List <Course>();
                foreach (var course in selectedCourses)
                {
                    newInstructor.Courses.Add(_courses.Single(c => c.CourseID == int.Parse(course)));
                }
            }

            #region snippet_TryUpdate
            if (await TryUpdateModelAsync <InstructorWithCollection>(
                    newInstructor,
                    "Instructor",
                    i => i.FirstMidName, i => i.LastName, i => i.HireDate))
            {
                _instructorsInMemoryStore.Add(newInstructor);
                return(RedirectToPage("./Index"));
            }
            PopulateAssignedCourseData(newInstructor);
            return(Page());

            #endregion
        }
Beispiel #2
0
        public IActionResult OnGet()
        {
            var instructor = new InstructorWithCollection();

            instructor.Courses = new List <Course>();

            // Provides an empty collection for the foreach loop
            // foreach (var course in Model.AssignedCourseDataList)
            // in the Create Razor page.
            PopulateAssignedCourseData(instructor);
            Instructor = instructor;
            return(Page());
        }
Beispiel #3
0
        public IActionResult OnGet(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Instructor = _instructorsInMemoryStore.FirstOrDefault(m => m.ID == id);

            if (Instructor == null)
            {
                return(NotFound());
            }
            PopulateAssignedCourseData(Instructor);
            return(Page());
        }
Beispiel #4
0
        public void PopulateAssignedCourseData(InstructorWithCollection instructor)
        {
            var allCourses        = _courses;
            var instructorCourses = new HashSet <int>(
                instructor.Courses.Select(c => c.CourseID));

            AssignedCourseDataList = new List <AssignedCourseData>();
            foreach (var course in allCourses)
            {
                AssignedCourseDataList.Add(new AssignedCourseData
                {
                    CourseID = course.CourseID,
                    Title    = course.Title,
                    Assigned = instructorCourses.Contains(course.CourseID)
                });
            }
        }
Beispiel #5
0
        public void UpdateInstructorCourses(string[] selectedCourses, InstructorWithCollection instructorToUpdate)
        {
            if (selectedCourses == null)
            {
                instructorToUpdate.Courses = new List <Course>();
                return;
            }
            if (instructorToUpdate.Courses == null)
            {
                instructorToUpdate.Courses = new List <Course>();
            }
            var selectedCoursesHS = new HashSet <string>(selectedCourses);
            var instructorCourses = new HashSet <int>
                                        (instructorToUpdate.Courses.Select(c => c.CourseID));

            foreach (var course in _courses)
            {
                if (selectedCoursesHS.Contains(course.CourseID.ToString()))
                {
                    if (!instructorCourses.Contains(course.CourseID))
                    {
                        instructorToUpdate.Courses.Add(
                            new Course
                        {
                            CourseID = course.CourseID,
                            Title    = course.Title,
                            Credits  = course.Credits
                        });
                    }
                }
                else
                {
                    if (instructorCourses.Contains(course.CourseID))
                    {
                        Course courseToRemove
                            = instructorToUpdate
                              .Courses
                              .SingleOrDefault(i => i.CourseID == course.CourseID);
                        instructorToUpdate.Courses.Remove(courseToRemove);
                    }
                }
            }
        }