public ActionResult Comment(Comment newComment) { Post post = new Post(); using (BlogMvcContext db = new BlogMvcContext()) { post = db.Posts .Include(p => p.Comments) .Include(p => p.Category) .Include(p => p.User) .Where(p => p.Id == newComment.Post_Id) .FirstOrDefault(); } if (!this.ModelState.IsValid) { return(View("Details", post)); } using (BlogMvcContext db = new BlogMvcContext()) { newComment.Created = DateTime.Now; db.Comments.Add(newComment); db.SaveChanges(); } return(RedirectToAction("Details", new { slug = post.Slug })); }
public ActionResult Create([Bind(Include = "Id,Category_Id,User_Id,Name,Slug,Content")] Post post) { if (ModelState.IsValid) { post.Created = DateTime.Now; db.Posts.Add(post); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.Category_Id = new SelectList(db.Categories, "Id", "name", post.Category_Id); ViewBag.User_Id = new SelectList(db.Users, "Id", "Username", post.User_Id); // On invalide le cache de la sidebar HttpContext.Cache.Remove("Post"); return(View(post)); }