public ActionResult PostComment(EducationPlanCommentViewModel viewModel) { EducationPlanCommentModel model = Mapper.Map<EducationPlanCommentModel>(viewModel); using (var repository = new EducationPlanRepository()) { repository.CreateCommentForEducationPlan(model); repository.Save(); return RedirectToAction("Details", new { id = model.EducationPlanId }); } }
public ActionResult Create(IList<EducationPlanToHeadlineViewModel> viewModels) { //Checks that all fields has been supplied by the user. if (ModelState.IsValid) { //Gets the education plan from the session state. var educationPlanViewModel = (EducationPlanViewModel)Session["EducationPlan"]; EducationPlanModel educationPlanModel; if (educationPlanViewModel != null) { educationPlanModel = Mapper.Map<EducationPlanModel>(educationPlanViewModel); /*Saves the info abut the education plan to the database the EducationPlanModel now gets an id.*/ using (var repository = new EducationPlanRepository()) { repository.CreateInfoAboutEducationPlan(educationPlanModel); repository.Save(); } //That id is set for each of the junction table entries. foreach (EducationPlanToHeadlineViewModel viewModel in viewModels) { viewModel.EducationPlanId = educationPlanModel.Id; } List<EducationPlanToHeadlineModel> models = viewModels.Select(Mapper.Map<EducationPlanToHeadlineModel>).ToList(); /*Sets the headline-proprtie to null so that it isn't saved once more in the database because it is already there.*/ models.ForEach(m => m.Headline = null); //Saves the education plan to the database. using (var repository = new EducationPlanRepository()) { repository.CreateEducationPlan(models); repository.Save(); } return RedirectToAction("Index"); } } //Not all fields have been supplied to the user and the errors are presented to him or her. ModelState.AddModelError("", "Something went wrong"); return View(viewModels); }
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"); }