public ActionResult Add(LightingDesign model) { try { if (ModelState.IsValid) { var insertedModel = uow.LightingDesignRepository.Insert(model); uow.Save(); return RedirectToAction("Details", new { id = insertedModel.Id }); } } catch (DbEntityValidationException ex) { foreach (var error in ex.EntityValidationErrors) { foreach (var innerError in error.ValidationErrors) { ModelState.AddModelError(innerError.PropertyName, innerError.ErrorMessage); } } } return View(model); }
// GET: LPD/Add public ActionResult Add() { LightingDesign model = new LightingDesign(); model.Submitted = model.RequiredBy = DateTime.Now.Date; return View(model); }
public ActionResult Add(ProjectAddViewModel viewModel, bool ignoreSimilarProjects = false) { Project model = viewModel.Project; if (ModelState.IsValid) { if (!ignoreSimilarProjects) { // Check for duplicate projects // Manually load lazily loaded fields to create more accurate results model.Sector = uow.SectorRepository.Get(s => s.Id.Equals(model.SectorId)).FirstOrDefault(); model.Type = uow.ProjectTypeRepository.Get(t => t.Id.Equals(model.TypeId)).FirstOrDefault(); model.Status = uow.ProjectStatusRepository.Get(s => s.Code.Equals(model.StatusCode)).FirstOrDefault(); var existingProjects = uow.ProjectRepository.Get(p => !p.StatusCode.Equals("D")); var similarProjects = new Dictionary<Tuple<int, string>, double>(); foreach (var project in existingProjects) { var similarity = DistanceTo(model, project); if (similarity > 0.7) //TODO: Need to find a good boundary, also make lazily loaded fields reliable in these results { similarProjects.Add(Tuple.Create(project.Id, project.Name), similarity); } } if (similarProjects.Count > 0) { // Redirect to project comparison page if any are found ModelState.AddModelError("PossibleDuplicate", "Similar projects found; review and then re-submit if appropriate."); PrepareModelForView(model); viewModel.SimilarProjects = similarProjects; return View(viewModel); } } var insertedProject = uow.ProjectRepository.Insert(model); try { model.CreationDate = DateTime.Now; if (insertedProject.LightingDesignRequired) { var lpd = new LightingDesign(); lpd.ProjectId = insertedProject.Id; lpd.Submitted = DateTime.Now.Date; lpd.RequiredBy = insertedProject.DesignRequiredBy ?? DateTime.Now.Date; var insertedLpd = uow.LightingDesignRepository.Insert(lpd); } uow.Save(); return RedirectToAction("Detail", new { id = insertedProject.Id }); } catch (DbEntityValidationException ex) { foreach (var error in ex.EntityValidationErrors) { foreach (var innerError in error.ValidationErrors) { ModelState.AddModelError(innerError.PropertyName, innerError.ErrorMessage); } } } } if (model.Types == null || model.StatusCodes == null || model.Reasons == null || model.Sector == null) { PrepareModelForView(model); } return View(viewModel); }