public void AddBlog(AddBlogBm bm, string strCurrentUserId) { ApplicationUser user = this.Context.Users.Find(strCurrentUserId); BlogCategory category = this.Context.BlogCategories.FirstOrDefault(b => b.Title == bm.Category); Blog model = new Blog { UploadDate = DateTime.Now, Content = bm.Content, Title = bm.Title, Author = user, Category = category }; this.Context.Blogs.Add(model); this.Context.SaveChanges(); }
public ActionResult Add(AddBlogBm bm) { if (!this.ModelState.IsValid) { return(this.View(bm)); } try { var strCurrentUserId = User.Identity.GetUserId(); this.service.AddBlog(bm, strCurrentUserId); return(this.RedirectToAction("All")); } catch (DbEntityValidationException ex) { var error = ex.EntityValidationErrors.First().ValidationErrors.First(); this.ModelState.AddModelError(error.PropertyName, error.ErrorMessage); AddBlogVm vm = new AddBlogVm { Categories = this.blogService.GetAllCategories() }; return(this.View(vm)); } }