Esempio n. 1
0
        public ActionResult Add(StudentVM studentVM)
        {
            studentVM.Student.Courses = new List <Course>();

            foreach (var id in studentVM.SelectedCourseIds)
            {
                studentVM.Student.Courses.Add(CourseRepository.Get(id));
            }

            studentVM.Student.Major = MajorRepository.Get(studentVM.Student.Major.MajorId);



            //if (studentVM.Student.Major.MajorName == null)
            //{
            //    ModelState.Remove("E")
            //}

            if (!string.IsNullOrEmpty(studentVM.Student.LastName) && !string.IsNullOrEmpty(studentVM.Student.FirstName) &&
                studentVM.Student.GPA >= 0.0M && studentVM.Student.GPA <= 4.0M)
            {
                ModelState.Where(x => x.Value.Errors.Count > 0).Select(d => d.Key).ToList().ForEach(g => ModelState.Remove(g));
            }

            if (ModelState.IsValid)
            {
                StudentRepository.Add(studentVM.Student);
                return(RedirectToAction("List"));
            }
            else
            {
                studentVM.SetCourseItems(CourseRepository.GetAll());
                studentVM.SetMajorItems(MajorRepository.GetAll());
                return(View(studentVM));
            }
        }
Esempio n. 2
0
        public ActionResult Edit(StudentVM studentVM)
        {
            if (ModelState.IsValid)
            {
                studentVM.Student.Courses = new List <Course>();

                foreach (var id in studentVM.SelectedCourseIds)
                {
                    studentVM.Student.Courses.Add(CourseRepository.Get(id));
                }

                studentVM.Student.Major = MajorRepository.Get(studentVM.Student.Major.MajorId);


                StudentRepository.Edit(studentVM.Student);
                return(RedirectToAction("List"));
            }
            else
            {
                studentVM.SetCourseItems(CourseRepository.GetAll());
                studentVM.SetMajorItems(MajorRepository.GetAll());
                return(View("Edit", studentVM));
            }
        }
Esempio n. 3
0
        public ActionResult Add(StudentVM model)
        {
            if (ModelState.IsValid)
            {
                model.Student.Courses = new List <Course>();

                foreach (var id in model.SelectedCourseIds)
                {
                    model.Student.Courses.Add(CourseRepository.Get(id));
                }

                model.Student.Major = MajorRepository.Get(model.Student.Major.MajorId);

                StudentRepository.Add(model.Student);

                return(RedirectToAction("List"));
            }
            else
            {
                model.SetCourseItems(CourseRepository.GetAll());
                model.SetMajorItems(MajorRepository.GetAll());
                return(View(model));
            }
        }
Esempio n. 4
0
        public ActionResult Add(StudentVM studentVM)
        {
            bool isInt;

            if (ModelState.IsValidField("Student"))
            {
                studentVM.Student.Courses = new List <Course>();

                foreach (var id in studentVM.SelectedCourseIds)
                {
                    studentVM.Student.Courses.Add(CourseRepository.Get(id));
                }

                studentVM.Student.Major = MajorRepository.Get(studentVM.Student.Major.MajorId);

                if (string.IsNullOrEmpty(studentVM.Student.FirstName))
                {
                    ModelState.AddModelError("Student", "Student First Name Cannot Be Blank!");
                }
                else if (studentVM.Student.FirstName.Any(char.IsDigit))
                {
                    ModelState.AddModelError("Student", "Student First Name Cannot Contain Numbers!");
                }
                else if (string.IsNullOrEmpty(studentVM.Student.LastName))
                {
                    ModelState.AddModelError("Student", "Student Last Name Cannot Be Blank!");
                }
                else if (studentVM.Student.LastName.Any(char.IsDigit))
                {
                    ModelState.AddModelError("Student", "Student Last Name Cannot Contain Numbers!");
                }
                else if (string.IsNullOrEmpty(studentVM.Student.Major.ToString()))
                {
                    ModelState.AddModelError("Student", "You Must Select a Major!");
                }
                else if (studentVM.Student.GPA <= 0)
                {
                    ModelState.AddModelError("Student", "Student GPA Cannot be 0!");
                }
                else if (studentVM.Student.GPA > 4)
                {
                    ModelState.AddModelError("Student", "Student GPA Must Be Between 0.1 and 4.0");
                }
                else if (studentVM.Student.Courses.Count == 0)
                {
                    ModelState.AddModelError("Student", "Student Must Take At Least One Course!");
                }
                else
                {
                    var totalStudents = StudentRepository.GetAll().Count();

                    studentVM.Student.StudentId = totalStudents + 1;

                    StudentRepository.Add(studentVM.Student);

                    return(RedirectToAction("List"));
                }

                var viewModel = new StudentVM();
                viewModel.SetCourseItems(CourseRepository.GetAll());
                viewModel.SetMajorItems(MajorRepository.GetAll());

                return(View(viewModel));
            }

            var viewModel2 = new StudentVM();

            viewModel2.SetCourseItems(CourseRepository.GetAll());
            viewModel2.SetMajorItems(MajorRepository.GetAll());

            return(View(viewModel2));
        }
Esempio n. 5
0
        public ActionResult Edit(StudentVM model)
        {
            Student student = new Student();

            student.StudentId = model.Student.StudentId;
            student.FirstName = model.Student.FirstName;


            if (string.IsNullOrEmpty(student.FirstName))
            {
                ModelState.AddModelError("Student", "Student Last Name Cannot Be Blank!");
            }
            else if (model.Student.FirstName.Any(char.IsDigit))
            {
                ModelState.AddModelError("Student", "Student First Name Cannot Contain Numbers!");
            }

            student.LastName = model.Student.LastName;

            if (string.IsNullOrEmpty(student.LastName))
            {
                ModelState.AddModelError("Student", "Student Last Name Cannot Be Blank!");
            }
            else if (student.LastName.Any(char.IsDigit))
            {
                ModelState.AddModelError("Student", "Student Last Name Cannot Contain Numbers!");
            }

            student.GPA = model.Student.GPA;

            if (student.GPA == 0)
            {
                ModelState.AddModelError("Student", "Student GPA Cannot Be Zero!");
            }
            if (student.GPA > 4)
            {
                ModelState.AddModelError("Student", "Student GPA Must Be Between 0.1 and 4.0");
            }

            student.Major = MajorRepository.Get(model.Student.Major.MajorId);

            student.Courses = model.Student.Courses;

            if (string.IsNullOrEmpty(model.SelectedCourseIds.ToString()))
            {
                ModelState.AddModelError("Student", "Student Must Take At Least One Course!");
            }

            Address address = new Address();
            State   state   = new State();

            if (model.Student.Address.AddressId == 0)
            {
                address.AddressId = student.StudentId;
            }
            else
            {
                address.AddressId = model.Student.Address.AddressId;
            }

            address.Street1 = model.Student.Address.Street1;

            address.Street2 = model.Student.Address.Street2;

            address.City = model.Student.Address.City;

            state.StateName = model.Student.Address.State.StateName;

            var stateNames = (from states in StateRepository.GetAll()
                              select states.StateName).ToList();

            if (state.StateName != null && !stateNames.Contains(state.StateName))
            {
                ModelState.AddModelError("Student", "Error: State is Not a Valid Option in Database");
            }

            var stateAbb = (from states in StateRepository.GetAll()
                            where states.StateName == model.Student.Address.State.StateName
                            select states.StateAbbreviation).FirstOrDefault();

            state.StateAbbreviation = stateAbb;

            address.PostalCode = model.Student.Address.PostalCode;

            if (address.PostalCode != null && address.PostalCode.Length != 5)
            {
                ModelState.AddModelError("Student", "Please Enter a Five Digit Zip Code");
            }

            address.State = state;

            student.Address = address;

            if (ModelState.IsValid)
            {
                StudentRepository.Delete(model.Student.StudentId);

                StudentRepository.Add(student);

                return(RedirectToAction("List"));
            }
            else
            {
                var viewModel = new StudentVM();
                viewModel.SetCourseItems(CourseRepository.GetAll());
                viewModel.SetMajorItems(MajorRepository.GetAll());

                return(View(viewModel));
            }
        }