Exemple #1
0
        public ActionResult AddComment([Bind(Include = "CommentText")] ArticleCommentsModel commentsModel)
        {
            var ReturnUrl     = Request.Form["ReturnUrl"];
            var ArticleID     = Request.Form["ArticleID"];
            var UserEmail     = Session["sessionEmail"];
            var CommenterName = Request.Form["CommenterName"];

            if ((CommenterName != "" && CommenterName.Length < 3) || (commentsModel.CommentText.Length < 2))
            {
                TempData["ErrorMessage"] = "Invalid inputs.";
                if (!string.IsNullOrEmpty(ReturnUrl))
                {
                    return(Redirect(ReturnUrl));
                }
                return(RedirectToAction("Index", "Home"));
            }

            bool IsReply = Convert.ToBoolean(Convert.ToInt32(Request.Form["IsReply"]));

            if (IsReply)
            {
                commentsModel.ReplyCommentID = Int32.Parse(Request.Form["ReplyCommentID"]);
            }

            if (ModelState.IsValid)
            {
                commentsModel.ArticleID = Int32.Parse(ArticleID);
                commentsModel.Name      = CommenterName;
                if (UserEmail != null)
                {
                    commentsModel.UserEmail = UserEmail.ToString();
                }
                commentsModel.IsReply         = IsReply;
                commentsModel.UniqueCommentID = AppFunctions.RandomString(2) + AppFunctions.RandomInt(8) + AppFunctions.RandomString(2);

                db.ArticleComments.Add(commentsModel);
                db.SaveChanges();
                TempData["SuccessMessage"] = "Comment added.";

                if (!string.IsNullOrEmpty(ReturnUrl))
                {
                    return(Redirect(ReturnUrl));
                }
                return(RedirectToAction("Index", "Home"));
            }
            else
            {
                StringBuilder result = new StringBuilder();
                foreach (var item in ModelState)
                {
                    string key    = item.Key;
                    var    errors = item.Value.Errors;

                    foreach (var error in errors)
                    {
                        result.Append(key + " " + error.ErrorMessage + "#");
                    }
                }
                TempData["ErrorMessage"] = "Opps!an  error occured. " + result.ToString();

                if (!string.IsNullOrEmpty(ReturnUrl))
                {
                    return(Redirect(ReturnUrl));
                }
                return(View("Index", "Home"));
            }
        }