Ejemplo n.º 1
0
        public ActionResult AddPersonToCourse(PersonViewModel model, int courseId)
        {
            var course = _courseRepo.GetCourseById(courseId);

            ViewBag.CourseId = courseId;
            if (ModelState.IsValid)
            {
                var person = _personRepo.GetPersonByFirstnameAndSecondname(model.Firstname, model.Secondname);
                if (person != null)
                {
                    _courseRepo.AddPersonToCourse(course, person);
                    _courseRepo.Save();
                }
                else
                {
                    var name = model.Firstname + " " + model.Secondname;
                    person = new Person {
                        Firstname = model.Firstname, Secondname = model.Secondname, Name = name
                    };
                    _personRepo.Create(person);
                    _personRepo.Save();
                    _courseRepo.AddPersonToCourse(course, person);
                    _courseRepo.Save();
                }
                return(RedirectToAction("Index"));
            }
            return(View(model));
        }