public ActionResult Edit(SolutionViewModel solution) { try { if (ModelState.IsValid) { Solution editedSolution = _solutionService.GetSolution(solution.ID); editedSolution.Author = solution.Author; editedSolution.Description = solution.Description; editedSolution.FilePath = solution.FilePath.Trim(); editedSolution.Name = solution.Name.Trim(); editedSolution.SolutionTypeID = solution.SolutionTypeID; editedSolution.Keys.Clear(); editedSolution.Keys = _utilityService.ResolveKeys(solution.Keys); // TODO: Update keys editedSolution.Website = solution.Website; //editedSolution.DateRegistered = DateTime.Now; // TODO: Dateupdated try { _solutionService.SaveSolution(); } catch (DbEntityValidationException ex) { var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); // Combine the original exception message with the new one. var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); // Throw a new DbEntityValidationException with the improved exception message. throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors); } SetSolutionTypes(solution); return View(solution).WithSuccess(solution.Name + " succesfully updated."); } else { ModelState.AddModelError("", "Validation errors occured."); SetSolutionTypes(solution); return View(solution); } } catch (Exception ex) { SetSolutionTypes(solution); return View(solution).WithError(ex.Message); } }
private void SetSolutionTypes(SolutionViewModel selectedSolution) { var solutionTypes = _solutionTypeService.GetSolutionTypes(); ViewBag.SolutionTypeID = new SelectList(solutionTypes, "ID", "Type", selectedSolution.SolutionTypeID); if(string.IsNullOrEmpty(selectedSolution.SolutionTypeType)) { selectedSolution.SolutionTypeType = solutionTypes .FirstOrDefault(t => t.ID == selectedSolution.SolutionTypeID).Type; } ViewBag.SelectedType = selectedSolution; }