/// <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; }
public JsonResult Index(StudentViewModel viewModel) { try { // Popupate viewModel = PopulateViewModel(viewModel); // Validate if (!ModelState.IsValid) return Json(new {status = "error", message = "Validation Errors"}); // Create the Student (Bind) var student = new StudentFromViewModelFactory(viewModel).GetStudent(); // Add the student to the student table _courseService.AddStudentToCourseOffering(student, viewModel.SelectedCourseOffering); // Return success and pass along a message and the course offering ID return Json( new { status = "success", message = $"{student.StudentName} has been added", id = viewModel.SelectedCourseOffering }); } catch (CourseOfferingDoesNotExistException) { return Json(new {status = "error", message = "Course Offering Does not exist."}); } catch (StudentAlreadyInCourseOfferingException) { return Json(new {status = "error", message = "Student is already in this Course Offering"}); } }
/// <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; }