コード例 #1
0
        public ActionResult AddComment(CommentViewModel comment)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("CustomError",
                                         "Comments is Required!");
            }
            else
            {
                var entity = comment.CreateComment();
                entity.UserId = this.Data.Users.All()
                                .Where(u => u.UserName == comment.Author)
                                .Select(u => u.Id)
                                .FirstOrDefault();

                this.Data.Comments.Add(entity);
                this.Data.SaveChanges();
            }

            var comments = this.Data.Comments.All()
                           .Where(c => c.FilmId == comment.FilmId)
                           .Include(u => u.User)
                           .OrderByDescending(c => c.Id)
                           .Select(CommentViewModel.FromComment)
                           .ToList();

            return(PartialView("_CommentsPartial", comments));
        }