public ActionResult Create(CommentBindingModel model)
        {
            if (model == null || !this.ModelState.IsValid)
            {
                this.AddNotification("Invalid input data", NotificationType.ERROR);
                return this.RedirectToAction("Details", "Snippets");
            }

            var userId = this.User.Identity.GetUserId();

            var comment = new Comment
                {
                    AuthorId = userId,
                    Content = model.Content,
                    CreatedOn = DateTime.Now,
                    SnippetId = model.SnippetId
                };

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

            var commentView = this.Data.Comments.GetAll()
                                  .Where(c => c.Id == comment.Id)
                                  .ProjectTo<CommentViewModel>()
                                  .FirstOrDefault();

            this.AddNotification("Your Comment was accepted", NotificationType.SUCCESS);
            return this.PartialView("_CommentRow", commentView);
        }
Example #2
0
        public ActionResult Add(CommentBindingModel model)
        {
            string urlAbsolute = this.Request.UrlReferrer.AbsolutePath;
            var snippetId = int.Parse(Regex.Replace(urlAbsolute, @"[^\d+]", ""));

            if (model != null && this.ModelState.IsValid)
            {
                model.UserId = this.UserProfile.Id;
                model.CreationTime = DateTime.Now;
                model.SnippetId = snippetId;
                var comment = Mapper.Map<Comment>(model);
                this.Data.Comments.Add(comment);
                this.Data.SaveChanges();

                var commentFromDb = this.Data.Comments
                    .All()
                    .Where(c => c.Id == comment.Id)
                    .ProjectTo<CommentDetailsViewModel>()
                    .FirstOrDefault();

                return this.PartialView("DisplayTemplates/CommentDetailsViewModel", commentFromDb);
            }

            return new EmptyResult();
        }