コード例 #1
0
        public ActionResult Edit(SnippetViewModel snippet)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    Snippet editedSnippet = _snippetService.GetSnippet(snippet.ID);
                    editedSnippet.Name                  = snippet.Name.Trim();
                    editedSnippet.Description           = snippet.Description.Trim();
                    editedSnippet.GroupName             = snippet.GroupName.Trim();
                    editedSnippet.ProgrammingLanguageID = snippet.ProgrammingLanguageID;
                    editedSnippet.Keys.Clear();
                    editedSnippet.Keys    = _utilityService.ResolveKeys(snippet.Keys);
                    editedSnippet.Website = snippet.Website;
                    editedSnippet.Code    = snippet.Code;

                    try
                    {
                        _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);
                    }

                    SetLanguageTypes(snippet);

                    return(View(snippet).WithSuccess(snippet.Name + " succesfully updated."));
                }
                else
                {
                    ModelState.AddModelError("", "Validation errors occured.");

                    SetLanguageTypes(snippet);
                    return(View(snippet));
                }
            }
            catch (Exception ex)
            {
                SetLanguageTypes(snippet);
                return(View(snippet).WithError(ex.Message));
            }
        }
コード例 #2
0
        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));
            }
        }