public ActionResult AddComment(FormCollection formCollection)
        {
            PostComment newComment = new PostComment();
            TryUpdateModel<INewPostComment>(newComment, formCollection);
            newComment.PostedDate = DateTime.UtcNow;
            newComment.IPAddress = Request.UserHostAddress;
            if (ModelState.IsValid)
            {
                newComment.Body = Server.HtmlEncode(newComment.Body);
                _postCommentRepository.AddPostComment(newComment);
                _postCommentRepository.SaveChanges();

                if (!User.Identity.IsAuthenticated || (User.Identity.IsAuthenticated && _userRepository.GetUser(User.Identity.Name).Email.ToUpper() != newComment.Email.ToUpper()))
                {
                    try
                    {
                        SmtpClient smtpClient = new SmtpClient();
                        MailMessage notificationEmail = new MailMessage
                        {
                            IsBodyHtml = true,
                            Subject = string.Format("{0} posted a comment on CodeTunnel!", newComment.Author),
                            Body = string.Format(
                                "{0} posted a comment on your blog post titled <a href='{2}'>{1}</a>.<br /><br />{3}",
                                newComment.Author,
                                newComment.BlogPost.Title,
                                ConfigurationManager.AppSettings["FullUrl"] + Url.Action("Post", new { Id = newComment.PostID }),
                                newComment.Body)
                        };
                        notificationEmail.To.Add(newComment.BlogPost.User.Email);
                        smtpClient.Send(notificationEmail);
                    }
                    catch { }
                }

                if (Request.IsAjaxRequest())
                    return Json(new
                    {
                        Valid = true,
                        Html = MvcUtilities.Utilities.CommonUtils.RenderViewToString(this, "PostComments", newComment.BlogPost.PostComments.OrderBy(x => x.PostedDate), true)
                    });
                else
                    return RedirectToAction("Post", new { Id = newComment.PostID });
            }
            else
            {
                if (ModelState.ContainsKey("spam"))
                {
                    Variable spamCaught = this._variableRepository.GetVariable("spamCaught");
                    bool addNew = (spamCaught == null);
                    int spamCount = 0;
                    if (addNew)
                        spamCaught = new Variable { Name = "spamCaught" };
                    else
                        spamCount = int.Parse(spamCaught.Value);
                    spamCount++;
                    spamCaught.Value = spamCount.ToString();
                    if (addNew)
                        _variableRepository.AddVariable(spamCaught);
                    _variableRepository.SaveChanges();
                }

                if (!Request.IsAjaxRequest())
                {
                    var blogPost = _blogPostRepository.GetBlogPost(newComment.PostID);
                    var viewModel = new BlogPostViewModel
                    {
                        BlogPost = blogPost,
                        NewComment = newComment
                    };
                    return View("Post", viewModel);
                }
                else
                {
                    return Json(new
                    {
                        Valid = false,
                        Html = MvcUtilities.Utilities.CommonUtils.RenderViewToString(this, "NewComment", newComment, true)
                    });
                }
            }
        }
        /// <summary>
        /// Displays a single blog post.
        /// </summary>
        /// <param name="Id">The ID of the blog post.</param>
        /// <param name="postTitle">The friendly-url version of the blog title.</param>
        public ActionResult Post(int Id, string postTitle)
        {
            var blogPost = _blogPostRepository.GetBlogPost(Id);

            // Create friendly URL from blog post title and compare it to the friendly URL that was passed in.
            // If they do not match then redirect to the same action but with the correct title in the URL.
            string friendlyUrl = MvcUtilities.Utilities.CommonUtils.CreateFriendlyUrl(blogPost.Title);
            if (postTitle != friendlyUrl)
                return RedirectToAction("Post", new { Id = Id, postTitle = friendlyUrl });

            // Create a new post comment and pre-populate it with the current blog post ID, and if the user is
            // logged in then populate it with values from their profile to reduce typing when logged in users
            // comment on blog posts.
            PostComment newComment = new PostComment { PostID = blogPost.PostID };
            if (User.Identity.IsAuthenticated)
            {
                User currentUser = _userRepository.GetUser(User.Identity.Name);
                newComment.Author = currentUser.Username;
                newComment.Email = currentUser.Email;
                newComment.Website = currentUser.Website;
            }

            // Construct a view model and attach the current blog post and post comment.
            var viewModel = new BlogPostViewModel
            {
                BlogPost = blogPost,
                NewComment = newComment
            };

            // Render the "Post" view.
            return View(viewModel);
        }
 /// <summary>
 /// Deletes a post comment entity from the repository.
 /// </summary>
 /// <param name="postComment">The post comment to be deleted.</param>
 public void DeletePostComment(PostComment postComment)
 {
     _dataContext.PostComments.DeleteObject(postComment);
 }
 /// <summary>
 /// Adds a post comment entity to the repository.
 /// </summary>
 /// <param name="postComment">The post comment to be added.</param>
 public void AddPostComment(PostComment postComment)
 {
     _dataContext.PostComments.AddObject(postComment);
 }
 /// <summary>
 /// Create a new PostComment object.
 /// </summary>
 /// <param name="commentID">Initial value of the CommentID property.</param>
 /// <param name="postID">Initial value of the PostID property.</param>
 /// <param name="author">Initial value of the Author property.</param>
 /// <param name="postedDate">Initial value of the PostedDate property.</param>
 /// <param name="body">Initial value of the Body property.</param>
 public static PostComment CreatePostComment(global::System.Int32 commentID, global::System.Int32 postID, global::System.String author, global::System.DateTime postedDate, global::System.String body)
 {
     PostComment postComment = new PostComment();
     postComment.CommentID = commentID;
     postComment.PostID = postID;
     postComment.Author = author;
     postComment.PostedDate = postedDate;
     postComment.Body = body;
     return postComment;
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the PostComments EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToPostComments(PostComment postComment)
 {
     base.AddObject("PostComments", postComment);
 }