Ejemplo n.º 1
0
        public ActionResult BeginCreate(EducationPlanViewModel viewModel)
        {
            //Checks that all field in the form the user posted was provided by the user.
            if (ModelState.IsValid)
            {
                //Nothing can be persisted yet, because the education plan is not yet fully supplied by the user.
                Session["EducationPlan"] = viewModel;
                IEnumerable<HeadlineModel> models;
                using (var repository = new EducationPlanRepository())
                {
                    models = repository.GetHeadlinesForGroupId(viewModel.GroupId);
                    //if no hedlines are found http status code 404 is returned.
                    if (models == null)
                    {
                        return HttpNotFound();
                    }
                    //Translates the models into apropriate viewmodels
                    List<HeadlineViewModel> headlineViewModels = Mapper.Map<IEnumerable<HeadlineModel>, List<HeadlineViewModel>>(models);
                    //The actual text under each headline is saved in the junction table between the headline table and the education plan table.
                    IEnumerable<EducationPlanToHeadlineViewModel> educationToHeadlineViewModels =
                        headlineViewModels.Select(hvm => new EducationPlanToHeadlineViewModel { HeadLineId = hvm.Id, Headline = hvm });
                    return View("Create", educationToHeadlineViewModels.ToList());
                }

            }
            //if some fields were left empty the user is prompted to enter text into those fields as well.
            return View(viewModel);
        }
Ejemplo n.º 2
0
        public ActionResult Edit(EducationPlanViewModel viewModel)
        {
            using (var educationRepository = new EducationPlanRepository())
            using (var headlineRepository = new EducationPlanToHeadlineRepository())
            {
                educationRepository.Update(Mapper.Map<EducationPlanModel>(viewModel));
                foreach (var educationPlanHeadline in viewModel.EducationPlanToHeadline)
                {
                    headlineRepository.Update(Mapper.Map<EducationPlanToHeadlineModel>(educationPlanHeadline));
                }

                headlineRepository.Save();
                educationRepository.Save();
            }

            return RedirectToAction("Index");
        }