Пример #1
0
        public JsonResult Add(CourseOfferingViewModel viewModel)
        {
            // Populate VM
            viewModel = PopulateViewModel(viewModel);

            // Validate model state
            if (!ModelState.IsValid)
                return Json(new { error = true, message = "There were errors in the submission"});

            // Attempt to add the offering
            try
            {
                var offering = new CourseOffering()
                {
                    Course_CourseID = viewModel.SelectedCourseId,
                    Semester = viewModel.Semester.ToString(),
                    Year = viewModel.SelectedYear
                };

                _courseService.AddCourseOffering(offering);

            }
            catch (CourseOfferingExistsException)
            {
                // If the offering already exists, display error
                return Json(new { error = true, message = "Course offering already exists" });
            }

            return Json(new { error = false, message = "Course offering successfully created!" });
        }
Пример #2
0
        // Populate CourseOffering View Model
        public CourseOfferingViewModel PopulateViewModel(CourseOfferingViewModel viewModel)
        {
            viewModel.Courses = _courseService.GetAllCourses().OrderBy(c => c.CourseTitle).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;
        }
Пример #3
0
        public JsonResult List(string courseId)
        {
            // Get the selected course offerings
            var offerings = _courseService.GetAllCourseOfferings().Where(o => o.Course_CourseID == courseId).ToList();

            // Update view model with the list of offerings and selected course description
            var viewModel = new CourseOfferingViewModel()
            {
                CourseOfferings = offerings,
                Description = _courseService.GetCourseById(courseId).Description
            };

            // Return a Json object with the course description and the partial view
            return Json(new { description = viewModel.Description, partial = RenderPartialViewToString("_DisplayOfferings", viewModel) }, JsonRequestBehavior.AllowGet);
        }