public ActionResult Index(CourseViewModel selection) { // Save the course try { // Populate selection = PopulateViewModel(selection); // Validate if (!ModelState.IsValid) return View(selection); // Map var course = Mapper.Map<CourseViewModel, Course>(selection); // Add _courseService.AddCourse(course); // Respond Success("The Course has been created", true); } // The course name or number already exists catch (Exception ex) when (ex is CourseNumberNotUniqueException || ex is CourseNameNotUniqueException) { Danger("The course name or number already exist.", true); return View(selection); } // redirect back to the page index return RedirectToAction("Index"); }
/// <summary> /// Populates the view model. /// </summary> /// <param name="courseViewModel">The course view model.</param> /// <returns></returns> private CourseViewModel PopulateViewModel(CourseViewModel courseViewModel) { courseViewModel.Courses = _courseService.GetCourses; return courseViewModel; }