public ActionResult CreatePost(PostsDb post, HttpPostedFileBase image) { if (ModelState.IsValid) { PostsDbEntities context = new PostsDbEntities(); post.Time = this.HttpContext.Timestamp; post.Author = CurrentUser.UserName; BlogPostsManager bpManager = new BlogPostsManager(); post.Content = bpManager.AddLineBreaks(post.Content); if (image != null) { string imageName = post.Author; imageName += Guid.NewGuid(); string filePath = AppDomain.CurrentDomain.BaseDirectory; filePath += "Content\\PostImages\\" + imageName; image.SaveAs(filePath); post.Image = filePath; post.ImageMimeType = image.ContentType; } context.PostsDb.Add(post); try { context.SaveChanges(); } catch (DbEntityValidationException e) { #if false foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } #endif } return(Redirect("/Blog/Index")); } return(View("Error", new string[] { "TODO: Доделать. Ошибка связанная с созданием поста" })); }
public async Task <ActionResult> WriteComment(Comments comment, string returnUrl) { if (ModelState.IsValid) { CommentsEntities context = new CommentsEntities(); BlogPostsManager bpMan = new BlogPostsManager(); comment.Content = bpMan.AddLineBreaks(comment.Content); comment.Time = this.HttpContext.Timestamp; comment.Author = CurrentUser.UserName; comment.PostId = comment.Id; comment.Id = 0; context.Comments.Add(comment); try { await context.SaveChangesAsync(); } catch (DbEntityValidationException e) { #if false foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } #endif } return(Redirect(returnUrl ?? "Blog/Index")); } return(View("Error", new string[] { "TODO: Доделать. Ошибка связанная с созданием поста" })); }