public ActionResult Index(StudentViewModel viewModel) { try { // Popupate viewModel = PopulateViewModel(viewModel); // Validate if (!ModelState.IsValid) return View(viewModel); // Create the Student (Bind) var student = new StudentFromViewModelFactory(viewModel).GetStudent(); // Add the student to the student table _courseService.AddStudentToCourseOffering(student, viewModel.SelectedCourseOffering); Success($"{student.StudentName} has been added", true); } catch (CourseOfferingDoesNotExistException) { Danger("Course Offering Does not exist."); return View(viewModel); } catch (StudentAlreadyInCourseOfferingException) { Danger("Student is already in this Course Offering"); return View(viewModel); } // Return with success return RedirectToAction("Index"); }
/// <summary> /// Populates the view model. /// </summary> /// <param name="viewModel">The view model.</param> /// <returns></returns> public StudentViewModel PopulateViewModel(StudentViewModel viewModel) { // Format for Select list var options = _courseService.GetCourseOfferings.OrderBy(offering => offering.Course.CourseName).Select(offering => new { offering.CourseOfferingId, CourseOfferingTitle = $"{offering.Course.CourseNumber} {offering.Course.CourseName} ({offering.Semester} {offering.Year})" }); // Populate Select List viewModel.CourseOfferingSelection = new SelectList(options, "CourseOfferingId", "CourseOfferingTitle", viewModel.SelectedCourseOffering); // Return view Modal return viewModel; }
/// <summary> /// Initializes a new instance of the <see cref="StudentFromViewModelFactory"/> class. /// </summary> /// <param name="viewModel">The view model.</param> public StudentFromViewModelFactory(StudentViewModel viewModel) { _viewModel = viewModel; }