Esempio n. 1
0
        public ActionResult Add()
        {
            var viewModel = new ADDVM();

            viewModel.SetCourseItems(CourseRepository.GetAll());
            viewModel.SetMajorItems(MajorRepository.GetAll());
            return(View(viewModel));
        }
Esempio n. 2
0
        public ActionResult Add(ADDVM ADDVM)
        {
            if (ModelState.IsValid)
            {
                Student student = new Student();
                student.StudentId = ADDVM.StudentId;
                student.FirstName = ADDVM.FirstName;
                student.LastName  = ADDVM.LastName;
                student.GPA       = ADDVM.GPA;
                student.Courses   = ADDVM.Courses;
                student.Major     = MajorRepository.Get(ADDVM.MajorId);

                student.Courses = new List <Course>();

                foreach (var id in ADDVM.SelectedCourseIds)
                {
                    student.Courses.Add(CourseRepository.Get(id));
                }


                if (student.Courses.Count == 0)
                {
                    ModelState.AddModelError("Courses", "You MUST select a course.");
                    return(View(ADDVM));
                }
                StudentRepository.Add(student);

                return(RedirectToAction("List"));
            }
            else
            {
                var viewModel = ADDVM;//error here i think
                viewModel.SetCourseItems(CourseRepository.GetAll());
                viewModel.SetMajorItems(MajorRepository.GetAll());
                return(View(viewModel));
            }
        }