public ActionResult CodeSnippet(SnippetViewModel snippet) { try { if (ModelState.IsValid) { Snippet newSnippet = new Snippet(); newSnippet.Name = snippet.Name.Trim(); newSnippet.Description = snippet.Description.Trim(); newSnippet.GroupName = snippet.GroupName.ToLower().Trim(); newSnippet.Keys = _utilityService.ResolveKeys(snippet.Keys); newSnippet.DateCreated = DateTime.Now; if (!string.IsNullOrEmpty(snippet.Website)) { newSnippet.Website = snippet.Website.Trim(); } newSnippet.Code = snippet.Code; newSnippet.ProgrammingLanguageID = snippet.ProgrammingLanguageID; try { _snippetService.CreateSnippet(newSnippet); _snippetService.SaveSnippet(); } 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); } SetLanguagesTypes(); return(RedirectToAction("Code", "Snippets", new { id = newSnippet.ID }) .WithSuccess(newSnippet.Name + " saved successfully.")); } else { ModelState.AddModelError("", "Validation errors occured."); SetLanguagesTypes(); return(View(snippet)); } } catch (Exception ex) { SetLanguagesTypes(); return(View(snippet).WithError(ex.Message)); } }