public ActionResult Create()
 {
     var instructor = new Instructor();
     instructor.Courses = new List<Course>();
     PopulateAssignedCourseData(instructor);
     return View();
 }
 public void Update(Instructor student)
 {
     var existingInstructor = Database.Instructors.Find(x => x.Id == student.Id);
     Database.Instructors.Remove(existingInstructor);
     Database.Instructors.Add(student);
 }
 public void Create(Instructor student)
 {
     Database.Instructors.Add(student);
 }
        private void UpdateInstructorCourses(string[] selectedCourses, Instructor instructorToUpdate)
        {
            if (selectedCourses == null)
            {
                instructorToUpdate.Courses = new List<Course>();
                return;
            }

            var selectedCoursesHS = new HashSet<string>(selectedCourses);
            var instructorCourses = new HashSet<int>
                (instructorToUpdate.Courses.Select(c => c.Id));
            foreach (var course in _courseRepository.Get())
            {
                if (selectedCoursesHS.Contains(course.Id.ToString()))
                {
                    if (!instructorCourses.Contains(course.Id))
                    {
                        instructorToUpdate.Courses.Add(course);
                    }
                }
                else
                {
                    if (instructorCourses.Contains(course.Id))
                    {
                        instructorToUpdate.Courses.Remove(course);
                    }
                }
            }
        }
 private void PopulateAssignedCourseData(Instructor instructor)
 {
     var allCourses = _courseRepository.Get();
     var instructorCourses = new HashSet<int>(instructor.Courses.Select(c => c.Id));
     var viewModel = new List<AssignedCourseDataViewModel>();
     foreach (var course in allCourses)
     {
         viewModel.Add(new AssignedCourseDataViewModel
         {
             CourseID = course.Id,
             Title = course.Title,
             Assigned = instructorCourses.Contains(course.Id)
         });
     }
     ViewBag.Courses = viewModel;
 }