public ActionResult Add(StudentVM studentVM)
        {
            //if(studentVM.Student.Major.MajorId > 0)
            //{
            //    studentVM.Student.Major = MajorRepository.Get(studentVM.Student.Major.MajorId);
            //}
            //if (ModelState["Student.Major.MajorName"].Errors.Count == 1)
            //{
            //    ModelState["Student.Major.MajorName"].Errors.Clear();
            //}
            //if (ModelState["Student.Courses"].Errors.Count == 1)
            //{
            //    ModelState["Student.Courses"].Errors.Clear();
            //}
            if (!ModelState.IsValid)
            {
                studentVM.SetCourseItems(CourseRepository.GetAll());
                studentVM.SetMajorItems(MajorRepository.GetAll());
                return(View(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);

            StudentRepository.Add(studentVM.Student);

            return(RedirectToAction("List"));
        }
 public ActionResult Add()
 {
     var viewModel = new StudentVM();
     viewModel.SetCourseItems(CourseRepository.GetAll());
     viewModel.SetMajorItems(MajorRepository.GetAll());
     return View(viewModel);
 }
Example #3
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);
                int id2 = StudentRepository.Edit(studentVM.Student);
                return(RedirectToAction("List", new { id = id2 }));
            }
            else
            {
                var student   = StudentRepository.Get(studentVM.Student.StudentId);
                var viewModel = new StudentVM();
                viewModel.SetCourseItems(CourseRepository.GetAll());
                viewModel.SetMajorItems(MajorRepository.GetAll());

                viewModel.Student = student;

                foreach (var course in student.Courses)
                {
                    viewModel.SelectedCourseIds.Add(course.CourseId);
                }
                return(View("Edit", viewModel));
            }
        }
Example #4
0
        public ActionResult Edit(StudentVM studentVM)
        {
            if (ModelState.IsValid)
            {
                studentVM.Student.Courses = new List <Course>();
                foreach (var id in studentVM.CourseItems.Where(m => m.Selected))
                {
                    studentVM.Student.Courses.Add(CourseRepository.Get(int.Parse(id.Value)));
                }

                studentVM.Student.Major = MajorRepository.Get(studentVM.Student.Major.MajorId);
                StudentRepository.Edit(studentVM.Student);
                return(RedirectToAction("List"));
            }
            else
            {
                var student = StudentRepository.Get(studentVM.Student.StudentId);

                var viewModel = new StudentVM();
                viewModel.Student = student;

                viewModel.SetCourseItems(CourseRepository.GetAll());
                viewModel.SetMajorItems(MajorRepository.GetAll());
                viewModel.SetStateItems(StateRepository.GetAll());
                return(View("Edit", viewModel));
            }
        }
        public ActionResult EditStudent(int id)
        {
            var student = StudentRepository.Get(id);


            var model = new StudentVM();

            model.SetCourseItems(CourseRepository.GetAll());
            model.SetMajorItems(MajorRepository.GetAll());
            model.SetStateItems(StateRepository.GetAll());


            model.Student.FirstName = student.FirstName;
            model.Student.LastName  = student.LastName;
            model.Student.Major     = student.Major;
            model.Student.GPA       = student.GPA;
            model.Student.Courses   = student.Courses.ToList();
            model.Student.Address   = student.Address;
            model.Student.StudentId = student.StudentId;
            foreach (var studentCourse in student.Courses)
            {
                model.SelectedCourseIds.Add(studentCourse.CourseId);
            }



            return(View(model));
        }
Example #6
0
        public ActionResult Add(StudentVM studentVM)
        {
            if (ModelState.IsValid)
            {
                studentVM.Student.Courses = new List <Course>();

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

                if (studentVM.Student.Courses.Count == 0)
                {
                    ModelState.AddModelError("Courses", "Please select at least one course. ");
                    return(View(studentVM));
                }

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

                if (string.IsNullOrEmpty(studentVM.Student.Major.MajorName))
                {
                    ModelState.AddModelError("MajorName", "Please select a major.");
                    return(View(studentVM));
                }

                StudentRepository.Add(studentVM.Student);

                return(RedirectToAction("List"));
            }
            studentVM.SetCourseItems(CourseRepository.GetAll());
            studentVM.SetMajorItems(MajorRepository.GetAll());
            studentVM.SetStateItems(StateRepository.GetAll());
            return(View(studentVM));
        }
Example #7
0
        public ActionResult EditStudent(StudentVM aStudent)
        {
            if (ModelState.IsValid)
            {
                aStudent.SelectedCourseIds =
                    aStudent.CourseItems.Where(c => c.Selected).Select(c => int.Parse(c.Value)).ToList();

                aStudent.Student.Courses = new List <Course>();

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

                aStudent.Student.Major         = MajorRepository.Get(aStudent.Student.Major.MajorId);
                aStudent.Student.Address.State = StateRepository.Get(aStudent.Student.Address.State.StateName);

                StudentRepository.Edit(aStudent.Student);
                StudentRepository.SaveAddress(aStudent.Student.StudentId, aStudent.Student.Address);

                return(RedirectToAction("List"));
            }
            else
            {
                var s         = StudentRepository.Get(aStudent.Student.StudentId);
                var viewModel = new StudentVM();
                viewModel.SetCourseItems(CourseRepository.GetAll());
                viewModel.SetMajorItems(MajorRepository.GetAll());
                viewModel.SetStateItems(StateRepository.GetAll());
                viewModel.Student = s;

                return(View(viewModel));
            }
        }
        public ActionResult EditStudent(int id)
        {
            var viewModel = new StudentVM();

            viewModel.SetCourseItems(CourseRepository.GetAll());
            viewModel.SetMajorItems(MajorRepository.GetAll());
            viewModel.SetStateItems(StateRepository.GetAll());

            viewModel.Student = StudentRepository.Get(id);

            if (viewModel.Student.Courses == null)
            {
                viewModel.Student.Courses = new List <Course>();
            }

            foreach (Course course in viewModel.Student.Courses)
            {
                viewModel.SelectedCourseIds.Add(course.CourseId);
            }

            if (viewModel.Student.Address == null)
            {
                viewModel.Student.Address = new Address()
                {
                    AddressId = StudentRepository.GetAddressId()
                };
            }

            return(View(viewModel));
        }
        public ActionResult EditAddress(StudentVM studentVM)
        {
            if (!ModelState.IsValid)
            {
                studentVM.Student = StudentRepository.Get(studentVM.Student.StudentId);
                studentVM.SetStateItems(StateRepository.GetAll());
                return(View(studentVM));
            }

            studentVM.Student.Address.State     = StateRepository.Get(studentVM.Student.Address.State.StateAbbreviation);
            studentVM.Student.Address.AddressId = studentVM.Student.StudentId;
            StudentRepository.SaveAddress(studentVM.Student.StudentId, studentVM.Student.Address);


            var viewModel = new StudentVM();

            viewModel.Student = StudentRepository.Get(studentVM.Student.StudentId);
            viewModel.SetCourseItems(CourseRepository.GetAll());
            viewModel.SetMajorItems(MajorRepository.GetAll());
            if (viewModel.Student.Courses != null)
            {
                viewModel.SelectedCourseIds = viewModel.Student.Courses.Select(c => c.CourseId).ToList();
            }
            return(RedirectToAction("List"));
        }
        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.Delete(studentVM.Student.StudentId);
                StudentRepository.Add(studentVM.Student);

                return(RedirectToAction("List"));
            }
            else
            {
                var viewModel = new StudentVM();
                viewModel.Student = StudentRepository.Get(studentVM.Student.StudentId);
                viewModel.SetCourseItems(CourseRepository.GetAll());
                viewModel.SetMajorItems(MajorRepository.GetAll());
                viewModel.SetStateItems(StateRepository.GetAll());

                if (!(viewModel.Student.Courses == null))
                {
                    viewModel.SelectedCourseIds = viewModel.Student.Courses.Select(s => s.CourseId).ToList();
                }

                return(View(viewModel));
            }
        }
Example #11
0
        public ActionResult Edit(StudentVM studentVM)
        {
            var viewModel = new StudentVM();

            viewModel.SetCourseItems(CourseRepository.GetAll());
            viewModel.SetMajorItems(MajorRepository.GetAll());
            viewModel.SetStateItems(StateRepository.GetAll());

            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);

            StudentRepository.SaveAddress(studentVM.Student.StudentId, studentVM.Student.Address);

            if (string.IsNullOrEmpty(studentVM.Student.FirstName))
            {
                ModelState.AddModelError("FistName", "Please enter the students first name.");
            }

            if (string.IsNullOrEmpty(studentVM.Student.LastName))
            {
                ModelState.AddModelError("LastName", "Please enter the students last name.");
            }

            if (studentVM.Student.GPA > 4.0M || studentVM.Student.GPA < 0.0M)
            {
                ModelState.AddModelError("GPA", "Please enter the GPA between 0.0 and 4.0.");
            }

            if (string.IsNullOrEmpty(studentVM.Student.Address.Street1))
            {
                ModelState.AddModelError("Student.Address.Street1", "You must provide atleast one street address.");
            }

            if (string.IsNullOrEmpty(studentVM.Student.Address.City))
            {
                ModelState.AddModelError("Student.Address.City", "Please enter the city.");
            }

            if (string.IsNullOrEmpty(studentVM.Student.Address.PostalCode))
            {
                ModelState.AddModelError("Student.Address.PostalCode", "Please enter the postal code.");
            }

            if (ModelState.IsValid)
            {
                StudentRepository.Edit(studentVM.Student);
                return(RedirectToAction("List"));
            }
            return(View(viewModel));
        }
Example #12
0
        public ActionResult Delete(int id)
        {
            var viewModel = new StudentVM();

            viewModel.Student = StudentRepository.Get(id);
            viewModel.SetCourseItems(CourseRepository.GetAll());
            viewModel.SetMajorItems(MajorRepository.GetAll());
            return(View(viewModel));
        }
        public IActionResult Create()
        {
            var viewModel = new StudentVM();

            viewModel.SetCourseItems(_courseRepository.GetAllCourses());
            viewModel.SetMajorItems(_majorRepository.GetAllMajors());
            viewModel.SetStateItems(_stateRepository.GetAllStates());
            return(View(viewModel));
        }
Example #14
0
        public ActionResult EditStudent(int id)
        {
            Student model     = StudentRepository.Get(id);//need to return strudent vm after this step to allow course selection
            var     viewModel = new StudentVM();

            viewModel.Student = model;
            viewModel.SetCourseItems(CourseRepository.GetAll());
            viewModel.SetMajorItems(MajorRepository.GetAll());
            return(View(viewModel));
        }
Example #15
0
        public ActionResult Edit(int studentId)
        {
            var viewModel = new StudentVM();

            viewModel.Student = StudentRepository.Get(studentId);
            viewModel.SetCourseItems(CourseRepository.GetAll());
            viewModel.SetMajorItems(MajorRepository.GetAll());
            viewModel.SetStateItems(StateRepository.GetAll());
            return(View(viewModel));
        }
Example #16
0
        public ActionResult Add()
        {
            var viewModel = new StudentVM();

            viewModel.Student.Courses = new List <Course>();
            viewModel.SetCourseItems(CourseRepository.GetAll());
            viewModel.SetMajorItems(MajorRepository.GetAll());
            viewModel.SetStateItems(StateRepository.GetAll());
            return(View(viewModel));
        }
Example #17
0
        public ActionResult Edit(int id)
        {
            StudentVM studentVM = new StudentVM();

            studentVM.Student = StudentRepository.Get(id);
            studentVM.SetCourseItems(CourseRepository.GetAll());
            studentVM.SetMajorItems(MajorRepository.GetAll());
            studentVM.SetStateItems(StateRepository.GetAll());

            return(View(studentVM));
        }
Example #18
0
        public ActionResult Edit(int id)
        {
            var viewModel = new StudentVM();

            viewModel.Student           = StudentRepository.Get(id);
            viewModel.SelectedCourseIds = viewModel.Student.Courses.Select(s => s.CourseId).ToList();
            viewModel.SetCourseItems(CourseRepository.GetAll());
            viewModel.SetMajorItems(MajorRepository.GetAll());
            viewModel.SetStateItems(StateRepository.GetAll());
            return(View(viewModel));
        }
Example #19
0
        public ActionResult Edit(int id)
        {
            var model = StudentRepository.Get(id);

            model.Address.AddressId = id;
            var editModel = new StudentVM();

            editModel.Student = model;
            editModel.SetCourseItems(CourseRepository.GetAll());
            editModel.SetMajorItems(MajorRepository.GetAll());
            return(View(editModel));
        }
Example #20
0
        public ActionResult Edit(int id)
        {
            var studentVM = new StudentVM();

            studentVM.SetCourseItems(CourseRepository.GetAll());
            studentVM.SetMajorItems(MajorRepository.GetAll());
            studentVM.Student = StudentRepository.Get(id);
            studentVM.SelectedCourseIds.AddRange(studentVM.Student.Courses.Select(c => c.CourseId));
            //studentVM.Student.Address.State = StateRepository.Get(studentVM.Student.Address.State.StateAbbreviation);
            //studentVM.Student.Address = StudentRepository.SaveAddress(id);
            return(View(studentVM));
        }
        public ActionResult Edit(int id)
        {
            var student   = StudentRepository.Get(id);
            var viewModel = new StudentVM();

            {
                viewModel.SetCourseItems(CourseRepository.GetAll());
                viewModel.SetMajorItems(MajorRepository.GetAll());
                viewModel.Student = student;
            }
            return(View("Edit", viewModel));
        }
Example #22
0
        public ActionResult Edit(int studentId)
        {
            var model           = StudentRepository.Get(studentId);
            var selectedCourses = model.Courses.Select(p => p.CourseId);
            var viewModel       = new StudentVM();

            viewModel.Student = model;
            viewModel.SetCourseItems(CourseRepository.GetAll());
            viewModel.SetMajorItems(MajorRepository.GetAll());
            viewModel.SelectedCourseIds = selectedCourses.ToList();
            viewModel.SetStateItems(StateRepository.GetAll());
            return(View(viewModel));
        }
Example #23
0
        public ActionResult EditStudent(int id)
        {
            var       model   = StudentRepository.Get(id);
            StudentVM student = new StudentVM();

            student.SetCourseItems(CourseRepository.GetAll());
            student.SetMajorItems(MajorRepository.GetAll());
            student.SetStateItems(StateRepository.GetAll());
            student.Student           = model;
            student.Student.Address   = model.Address;
            student.SelectedCourseIds = model.Courses.Select(m => m.CourseId).ToList();

            return(View(student));
        }
        public ActionResult Delete(int studentId)
        {
            var viewModel = new StudentVM();

            viewModel.Student = StudentRepository.Get(studentId);
            viewModel.SetCourseItems(CourseRepository.GetAll());
            viewModel.SetMajorItems(MajorRepository.GetAll());
            if (viewModel.Student.Courses != null)
            {
                viewModel.SelectedCourseIds = viewModel.Student.Courses.Select(c => c.CourseId).ToList();
            }
            viewModel.SetStateItems(StateRepository.GetAll());
            return(View(viewModel));
        }
Example #25
0
        public ActionResult Add(StudentVM studentVM)
        {
            if (ModelState.IsValid)
            {
                //if (studentVM.Student.GPA<0 || studentVM.Student.GPA>5)
                //{
                //    var viewModel = new StudentVM();
                //    viewModel.SetCourseItems(CourseRepository.GetAll());
                //    viewModel.SetMajorItems(MajorRepository.GetAll());
                //    viewModel.SetStateItems(StateRepository.GetAll());

                //    return View("Add", viewModel);
                //}
                //else
                //{
                studentVM.Student.Courses = new List <Course>();
                studentVM.Student.Address = new List <Address>();
                AddressVM address = new AddressVM();
                foreach (var courseID in studentVM.SelectedCourseIds)
                {
                    studentVM.Student.Courses.Add(CourseRepository.Get(courseID));
                }

                studentVM.Student.Major = MajorRepository.Get(studentVM.Student.Major.MajorId);
                Student modelStudent = studentVM.Student;
                //Student student = new Student();
                //student.FirstName = modelStudent.FirstName;
                //student.LastName = modelStudent.LastName;
                //student.Courses = modelStudent.Courses;
                //student.GPA = modelStudent.GPA;
                //student.Major = modelStudent.Major;
                int id2 = StudentRepository.Add(modelStudent);
                address.Student = studentVM.Student;
                //StudentRepository.SaveAddress(studentVM.Student.StudentId, studentVM.Student.Address);

                //return RedirectToAction(/*"List"*/"AddAddress", address.Student/*.StudentId*/);
                return(RedirectToAction(/*"List"*/ "AddAddress", new { id = id2 } /*.StudentId*/));
                //}
            }
            else
            {
                var viewModel = new StudentVM();
                viewModel.SetCourseItems(CourseRepository.GetAll());
                viewModel.SetMajorItems(MajorRepository.GetAll());
                viewModel.SetStateItems(StateRepository.GetAll());

                return(View("Add", viewModel));
            }
        }
Example #26
0
 public ActionResult EditStudentVM(StudentVM studentVM)
 {
     if (ModelState.IsValid)
     {
         StudentRepository.SaveAddress(studentVM.Student.StudentId, studentVM.Student.Address);
         return(RedirectToAction("List"));
     }
     else
     {
         studentVM.Student = StudentRepository.Get(studentVM.Student.StudentId);
         studentVM.SetCourseItems(CourseRepository.GetAll());
         studentVM.SetMajorItems(MajorRepository.GetAll());
         return(View("Edit", studentVM));
     }
 }