public ActionResult Create(int id, CommentBindingModel model)
        {
            var snippet = this.Data.Snippets.Find(id);
            if (snippet == null)
            {
                TempData["messages_err"] = "Could not find snippet";
                return RedirectToAction("Index", "Home");
            }

            if (model != null && this.ModelState.IsValid)
            {
                var autorId = this.User.Identity.GetUserId();
                var comment = new Comment()
                {
                    Content = model.Content,
                    CreationDate = DateTime.Now,
                    Snippet = snippet,
                    Author = this.Data.Users.Find(autorId)
                };
                this.Data.Comments.Add(comment);
                this.Data.SaveChanges();
                TempData["messages_succ"] = "Successfully added comment";
            }
            return RedirectToAction("Details", "Snippets", new {id = id});
        }
Ejemplo n.º 2
0
        public ActionResult Create(CommentBindingModel model)
        {
            if (model != null && this.ModelState.IsValid)
            {
                var comment = Mapper.Map<Comment>(model);
                comment.CreatedAt = DateTime.Now;
                comment.AuthorId = this.UserProfile.Id;
                this.Data.Comments.Add(comment);
                this.Data.SaveChanges();
                var dbcomment = this.Data.Comments.Find(comment.Id);
                var viewModel = Mapper.Map<CommentViewModel>(dbcomment);
                return this.PartialView("DisplayTemplates/CommentForSnipperViewModel", new [] { viewModel });
            }

            return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Invalid input");
        }