// GET: Instructor/Create public ActionResult Create() { var instructor = new Instructor(); instructor.Courses = new List<Course>(); PopulateAssignedCourseData(instructor); return View(); }
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.CourseId)); foreach (var course in db.Courses) { if (selectedCoursesHs.Contains(course.CourseId.ToString())) { if (!instructorCourses.Contains(course.CourseId)) { instructorToUpdate.Courses.Add(course); } } else { if (instructorCourses.Contains(course.CourseId)) { instructorToUpdate.Courses.Remove(course); } } } }
private void PopulateAssignedCourseData(Instructor instructor) { var allCourses = db.Courses; var instructorCourses = new HashSet<int>(instructor.Courses.Select(c => c.CourseId)); var viewModel = new List<AssignedCourseData>(); foreach (var course in allCourses) { viewModel.Add(new AssignedCourseData { CourseId = course.CourseId, Title = course.Title, Assigned = instructorCourses.Contains(course.CourseId) }); } ViewBag.Courses = viewModel; }