예제 #1
0
        public ActionResult PostComment(SubmitCommentModel submitCommentModel)
        {
            if (ModelState.IsValid)
            {
                string userId = User.Identity.GetUserId();
                string userName = User.Identity.GetUserName();

                this.Data.Comments.Add(new Comment
                    {
                        LaptopId = submitCommentModel.LaptopId,
                        Content = submitCommentModel.Comment,
                        ApplicationUserId = userId
                    });

                this.Data.SaveChanges();

                var commentViewModel = new CommentViewModel { Author = userName, Content = submitCommentModel.Comment };

                return PartialView("_Comments", commentViewModel);
            }

            //var message = string.Join(" | ", ModelState.Values
            //            .SelectMany(v => v.Errors)
            //            .Select(e => e.ErrorMessage));
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest, ModelState.Values.First().ToString());
        }
        public JsonResult DestroyComment([DataSourceRequest] DataSourceRequest request, CommentViewModel comment)
        {
            this.Data.Comments.Delete(comment.Id);
            this.Data.SaveChanges();

            return Json(new[] { comment }.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }
예제 #3
0
        public ActionResult Comment(CommentViewModel comment)
        {
            if (User.Identity.IsAuthenticated && ModelState.IsValid)
            {
                var currentUserId = User.Identity.GetUserId();
                var newComment = new Comment()
                {
                    AuthorId = currentUserId,
                    Content = comment.Content,
                    laptopId = comment.Id
                };

                this.Data.Comments.Add(newComment);
                this.Data.SaveChanges();

                var currentUser = User.Identity.Name;

                return Content("<div class='span7 divider'>" +
                    "<p>Comment by " + currentUser + "</p>" +
                    "<p>" + comment.Content + "</p>" +
                    "</div>");
            }

            return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest, ModelState.Values.First().ToString());
        }
        public JsonResult UpdateComment([DataSourceRequest] DataSourceRequest request, CommentViewModel comment)
        {
            var commentDb = this.Data.Comments.GetById(comment.Id);

            commentDb.Content = comment.Content;
            this.Data.SaveChanges();

            return Json(new[] { comment }.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }
예제 #5
0
        public ActionResult Delete([DataSourceRequest] DataSourceRequest request, CommentViewModel comment)
        {
            if (ModelState.IsValid)
            {
                var commentForDelete = this.Data.Comments.GetById(comment.Id);
                this.Data.Comments.Delete(commentForDelete);
                this.Data.SaveChanges();
            }

            return Json(new[] { comment }.ToDataSourceResult(request, ModelState));
        }
예제 #6
0
        public ActionResult Edit([DataSourceRequest] DataSourceRequest request, CommentViewModel comment)
        {
            if (ModelState.IsValid)
            {
                var editedComment = this.Data.Comments.GetById(comment.Id);
                editedComment.Content = comment.Content;
                this.Data.Comments.Update(editedComment);
                this.Data.SaveChanges();
            }
            else
            {
                ModelState.AddModelError("Content", "Comment is invalid!");
            }

            return Json(new[] { comment }.ToDataSourceResult(request, ModelState));
        }
        public ActionResult PostComment(SubmitCommentModel commentModel)
        {
            if (ModelState.IsValid)
            {
                var username = this.User.Identity.GetUserName();
                var userId = this.User.Identity.GetUserId();

                this.Data.Comments.Add(new Comment()
                {
                    AuthorId = userId,
                    Content = commentModel.Comment,
                    LaptopId = commentModel.LaptopId, 
                });

                this.Data.SaveChanges();

                var viewModel = new CommentViewModel { AuthorUsername = username, Content = commentModel.Comment };
                return PartialView("_CommentPartial", viewModel);
            }

            return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest, ModelState.Values.First().ToString());
        }