コード例 #1
0
        // GET: Posts/Details/5
        public ActionResult Details(string blogName, int postId)
        {
            Post post = db.Posts.Find(postId);

            if (post == null)
            {
                return(HttpNotFound());
            }
            var blog   = db.Blogs.Find(post.BlogId);
            var layout = db.LayoutSettings.Find(blog.Id);
            var model  = new DisplayPost {
                Post = post, Blog = blog, LayoutSettings = layout
            };

            return(View("Themes/Default", model));
        }
コード例 #2
0
        // GET: Post/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(RedirectToAction("Index", "Home"));
            }
            DisplayPost postAndComments = new DisplayPost();
            Post        post            = new Post();

            //Get the post details
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                string     sql     = @"
                      SELECT Post.Id, Text, CreatedOn, CreatorId, FirstName+' '+LastName as Name
                      FROM Post join [User] on Post.CreatorId = [User].Id 
                      Where Post.Id = @PostId
                    ";
                SqlCommand command = new SqlCommand(sql, connection);
                command.Parameters.AddWithValue("@PostId", id);

                SqlDataReader reader = command.ExecuteReader();
                reader.Read();

                post.Id          = reader.GetInt32(0);
                post.Text        = reader.GetString(1);
                post.CreatedOn   = reader.GetDateTimeOffset(2);
                post.CreatorId   = reader.GetInt32(3);
                post.CreatorName = reader.GetString(4);
            }
            postAndComments.Post = post;
            //Get the comments for this post
            List <Comment> comments = new List <Comment>();

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                string     sql     = @"
                      SELECT Comment.Id, Post.Id as PostId, Comment.CreatorId, FirstName+' '+LastName as Name, Comment.CreatedOn, Comment.Text
                      FROM Comment join Post on (Comment.PostId = Post.Id) 
                      join [User] on Comment.CreatorId = [User].Id 
                      Where Post.Id = @PostId
                ";
                SqlCommand command = new SqlCommand(sql, connection);
                command.Parameters.AddWithValue("@PostId", id);

                SqlDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    Comment comment = new Comment();
                    comment.Id          = reader.GetInt32(0);
                    comment.PostId      = reader.GetInt32(1);
                    comment.CreatorId   = reader.GetInt32(2);
                    comment.CreatorName = reader.GetString(3);
                    comment.CreatedOn   = reader.GetDateTimeOffset(4);
                    comment.Text        = reader.GetString(5);
                    comments.Add(comment);
                }
            }
            postAndComments.Comments = comments;

            return(View(postAndComments));
        }