Пример #1
0
        public ActionResult AddComment(string commentBody, string username, string postId)
        {
            Comment comment = new Comment();
            Author author = new Author();

            author.Name = username;
            author.EmailAddress = username;
            comment.Content = commentBody;
            comment.Author = author;
            comment.PostId = int.Parse(postId);
            comment.CreatedDate = DateTime.Now;

            db.Comments.Add(comment);

            db.SaveChanges();

            if (Request.IsAjaxRequest())
            {
                return Json(new { username = username, commentBody = commentBody, commentDate = comment.CreatedDate.ToString() });
            }

            Post post = new Post();
            post = db.Posts.Find(int.Parse(postId));

            return View("Details", new { post = post });
        }
Пример #2
0
        public ActionResult Create(CreateCommentViewModel commentViewModel)
        {
            if (ModelState.IsValid)
            {
                Comment comment = new Comment();
                Author author = new Author();

                author.Name = commentViewModel.Username;
                author.EmailAddress = commentViewModel.EmailAddress;
                comment.Content = commentViewModel.content;
                comment.Author = author;
                comment.PostId = commentViewModel.postId;
                comment.CreatedDate = DateTime.Now;

                TryUpdateModel(comment);
                db.Comments.Add(comment);

                db.SaveChanges();
                return RedirectToAction("Details", "Post", new { postId = commentViewModel.postId });
            }
            return View("NewComment");
        }