Exemplo n.º 1
0
        public ActionResult AddComment(AddCommentViewModel vm)
        {
            if (ModelState.IsValid)
            {
                Comment comment = new Comment()
                {
                    ArticleId         = vm.ArticleId,
                    Content           = vm.Content,
                    ApplicationUserId = User.Identity.GetUserId(),
                    CommentTime       = DateTime.Now
                };
                db.Comments.Add(comment);
                db.SaveChanges();

                ApplicationUser       user   = db.Users.Find(comment.ApplicationUserId);
                CommentAddedViewModel result = new CommentAddedViewModel()
                {
                    CommentSuccess = "Your commend successfuly added.",
                    Author         = user.NickName,
                    Content        = comment.Content,
                    CommentId      = comment.Id,
                    CommentTime    = comment.CommentTime.ToString("g")
                };
                return(Json(result));
            }

            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json(new { ErrorMessage = "Please control your last message" }));
        }
Exemplo n.º 2
0
        public ActionResult AddComment(AddCommentViewModel vm)
        {
            if (ModelState.IsValid)
            {
                Comment comment = new Comment()
                {
                    ArticleId         = vm.ArticleId,
                    Content           = vm.Content,
                    ApplicationUserId = User.Identity.GetUserId(),
                    CommentTime       = DateTime.Now
                };
                db.Comments.Add(comment);
                db.SaveChanges();

                CommentAddedViewModel result = new CommentAddedViewModel()
                {
                    CommentSuccess = "Your comment added successfully",
                    Author         = User.Identity.GetNickname(),
                    Content        = comment.Content,
                    CommentId      = comment.Id,
                    CommentTime    = comment.CommentTime.Value.ToString("g")
                };

                return(Json(result));
            }

            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json(new { ErrorMessage = "Check your message" }));
        }