public ActionResult AddRelatedComment(Comment comment, int parent) { // Spam is not an error if (ModelState["isspam"] != null) { ModelState.Remove("isspam"); comment.IsSpam = true; } if (TryValidateModel(comment)) { comment.Ip = Request.UserHostAddress; comment.UserAgent = Request.UserAgent; if (!CodeFirstSecurity.IsAuthenticated) { AnonymousCommentViewModel user; if (comment.AnonymousUser != null) { user = AutoMapper.Mapper.Map <AnonymousUser, AnonymousCommentViewModel>(comment.AnonymousUser); } else { user = new AnonymousCommentViewModel(); } if (TryValidateModel(user)) { BlogServices.AddRelatedComment(comment, parent, null); if (BgResources.Send_Mail_When_Comment_Received) { Utilities.SendMail(BgResources.Email_UserName, String.Format(Resources.AppMessages.Comment_Received, Request.UrlReferrer.AbsoluteUri), "<h2>" + Resources.AppMessages.User + "</h2>" + "<p>" + comment.AnonymousUser.Username + "</p>" + "<p>" + comment.AnonymousUser.Email + "</p>" + "<p>" + comment.AnonymousUser.Web + "</p>" + "<p>" + comment.Ip + "</p>" + "<p>" + comment.UserAgent + "</p>" + "<p>" + (comment.IsSpam ? Resources.AppMessages.MarkedAsSpam : Resources.AppMessages.MarkedAsHam) + "</p>" + "<p>" + Request.UrlReferrer.AbsoluteUri + "</p>" + "<h2>" + Resources.AppMessages.Comment_Message + "</h2>" + "<div>" + comment.Message + "</div>", true); } if (comment.IsSpam) { return(Json(new { result = "warnings", warnings = new KeyValuePair <string, string>("spamdetected", Resources.AppMessages.SpamDetected) })); } else { return(Json(new { result = "ok" })); } } else { return(Json(new { result = "error", errors = ModelState.Where(s => s.Value.Errors.Count > 0).Select(s => new KeyValuePair <string, string>(s.Key, s.Value.Errors.First().ErrorMessage)).ToArray() })); } } else { User user = UserServices.FindEntityByIdentity(CodeFirstSecurity.CurrentUserId); BlogServices.AddRelatedComment(comment, parent, user); if (BgResources.Send_Mail_When_Comment_Received) { Utilities.SendMail(BgResources.Email_UserName, String.Format(Resources.AppMessages.Comment_Received, Request.Url.AbsoluteUri), "<h2>" + Resources.AppMessages.User + "</h2>" + "<p>" + user.Username + "</p>" + "<p>" + user.Email + "</p>" + "<p>" + comment.UserAgent + "</p>" + "<p>" + Request.Url.AbsoluteUri + "</p>" + "<h2>" + Resources.AppMessages.Comment_Message + "</h2>" + "<div>" + comment.Message + "</div>", true); } if (comment.IsSpam) { return(Json(new { result = "warnings", warnings = new KeyValuePair <string, string>("spamdetected", Resources.AppMessages.SpamDetected) })); } else { return(Json(new { result = "ok" })); } } } return(Json(new { result = "error", errors = ModelState.Where(s => s.Value.Errors.Count > 0).Select(s => new KeyValuePair <string, string>(s.Key, s.Value.Errors.First().ErrorMessage)).ToArray() })); }