Пример #1
0
        public ActionResult Create(PostTagViewModel vm, int id)
        {
            vm.Post        = _postRepository.GetPublishedPostById(id);
            vm.PostTagList = _tagRepository.GetTagsByPostId(id);
            vm.AllTags     = _tagRepository.GetAllTags();
            try
            {
                foreach (Tags t in vm.PostTagList)
                {
                    var postTag = _postTagRepository.GetPostTagbyPostWithTag(t.Id, id);
                    if (postTag != null)
                    {
                        _postTagRepository.DeletePostTag(postTag.Id);
                    }
                }


                if (vm.IsSelected != null)
                {
                    foreach (int tagId in vm.IsSelected)

                    {
                        var aPostTag = new PostTag()
                        {
                            PostId = id,
                            TagId  = tagId
                        };
                        _postTagRepository.AddTagToPost(aPostTag);
                    }
                    return(RedirectToAction("UserPostDetails", "Post", new { id = id }));
                }
                else
                {
                    return(RedirectToAction("UserPostDetails", "Post", new { id = id }));
                }
            }
            catch
            {
                return(View(vm));
            }
        }