Example #1
0
        public ActionResult Index(RegistrationModel registrationModel)
        {
            if (!String.IsNullOrWhiteSpace(registrationModel.UserIdText))
            {
                viewModel.CourseListItems = new List <SelectListItem>();
                if (registrationModel.SelectedCourseNumber != null)
                {
                    viewModel.SelectedCourseNumber = registrationModel.SelectedCourseNumber;
                    viewModel.UserIdText           = registrationModel.UserIdText;

                    Course selectedCourse = viewModel.Courses.Where(c => c.CourseNumber == viewModel.SelectedCourseNumber).Single();
                    generateCourseList();
                    if (!(viewModel.TotalCredits + selectedCourse.Credits > 9))
                    {
                        if (!viewModel.RegisteredCourses.Select(c => c.CourseNumber).Contains(selectedCourse.CourseNumber))
                        {
                            viewModel.RegisteredCourses.Add(new RegisteredCourse(viewModel.UserIdText, viewModel.SelectedCourseNumber));
                            viewModel.SaveChanges();
                            viewModel.UserMessage = $"{selectedCourse.CourseNumber} registered!";
                        }
                        else
                        {
                            viewModel.UserMessage = "Cannot select same course more than once.";
                        }
                    }
                    else
                    {
                        viewModel.UserMessage = "Cannot register for more than 9 credits!";
                    }
                }
                else
                {
                    viewModel.UserMessage = "Please select a course";
                }
                generateCourseList();
                return(View(viewModel));
            }
            else
            {
                viewModel.UserMessage = "Please enter user id.";
                generateCourseList();
                return(View(viewModel));
            }
        }