public ActionResult Add(PostTagsAddViewModel viewModel)
        {
            ValidatePostTags(viewModel);

            if (ModelState.IsValid)
            {
                var postTags = new PostTags()
                {
                    PostId = viewModel.PostId,
                    TagId  = viewModel.TagId,
                };

                _postTagsRepository.Add(postTags);

                TempData["Message"] = "Your tag was successfully added!";

                return(RedirectToAction("Edit", "Post", new { id = viewModel.PostId }));
            }


            viewModel.Post = _postRepository.Get(viewModel.PostId);
            viewModel.Init(_tagRepository);

            return(View(viewModel));
        }
 private void ValidatePostTags(PostTagsAddViewModel viewModel)
 {
     if (ModelState.IsValidField("PostId") &&
         ModelState.IsValidField("TagId"))
     {
         if (_postTagsRepository.PostHasTagAlready(viewModel.PostId, viewModel.TagId))
         {
             ModelState.AddModelError("TagId",
                                      "This tag already exists for this post.");
         }
     }
 }
        // GET: PostTags/Create
        public ActionResult Add(int postId)
        {
            var post = _postRepository.Get(postId);

            if (post == null)
            {
                return(HttpNotFound());
            }

            var viewModel = new PostTagsAddViewModel()
            {
                Post = post
            };

            viewModel.Init(_tagRepository);

            return(View(viewModel));
        }