コード例 #1
0
ファイル: PostController.cs プロジェクト: LaRoyalCrew/BlogMVC
        public ActionResult Details(string slug)
        {
            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.Slug == slug)
                    .FirstOrDefault();

            }



            return View(post);
        }
コード例 #2
0
ファイル: PostController.cs プロジェクト: LaRoyalCrew/BlogMVC
        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 });

        }