Exemplo n.º 1
0
        // Populate CourseOffering View Model
        public CourseOfferingViewModel PopulateViewModel(CourseOfferingViewModel viewModel)
        {
            viewModel.Courses = _courseService.GetAllCourses().OrderBy(c => c.Name).ToList();
            viewModel.CourseOfferings = _courseService.GetAllCourseOfferings();
            viewModel.CourseOfferings.Sort(new CourseOfferingComparer());
            viewModel.Years = Enumerable.Range(DateTime.Now.Year, 4).Select(i => new SelectListItem
            {
                Text = i.ToString(),
                Value = i.ToString()
            });

            return viewModel;
        }
Exemplo n.º 2
0
        public ActionResult Add(CourseOfferingViewModel input)
        {
            // Populate VM
            var viewModel = PopulateViewModel(input);

            // Validate model state
            if (!ModelState.IsValid)
                return View(viewModel);

            // Attempt to add the offering
            try
            {
                var offering = new CourseOffering(_courseService.GetCourseById(input.SelectedCourseId), input.Semester.ToString(), input.SelectedYear);
                _courseService.AddCourseOffering(offering);
                input.CourseOfferings.Add(offering);
            }
            catch (CourseOfferingExistsException)
            {
                // If the offering already exists, display error
                TempData["Error"] = "Course offering already exists!";
            }

            return RedirectToAction("Add", input);
        }