Пример #1
0
        // GET: PostTagController/Edit/5
        public ActionResult Edit(int id)
        {
            Post       post     = _postRepository.GetPublishedPostById(id);
            List <Tag> postTags = _tagRepository.GetPostTags(post.Id);
            List <Tag> allTags  = _tagRepository.GetAll();

            PostTagMgmtViewModel vm = new PostTagMgmtViewModel()
            {
                PostId   = id,
                PostTags = postTags,
                AllTags  = allTags
            };

            return(View(vm));
        }
Пример #2
0
        public ActionResult Edit(int id, string[] PostTagsChecks)
        {
            Post       post     = _postRepository.GetPublishedPostById(id);
            List <Tag> postTags = _tagRepository.GetPostTags(post.Id);
            List <Tag> allTags  = _tagRepository.GetAll();

            PostTagMgmtViewModel vm = new PostTagMgmtViewModel()
            {
                PostId   = id,
                PostTags = postTags,
                AllTags  = allTags
            };



            try
            {
                // Original Post Tag ID's
                List <int> OrgTagIds = postTags.Select(tag => tag.Id).ToList();
                // Newly-Set Post Tag ID's
                List <int> AdjTagIds = Array.ConvertAll(PostTagsChecks, int.Parse).ToList();
                // Tag ID's to remove
                List <int> deprecatedPostTags = OrgTagIds.Except(AdjTagIds).ToList();
                // Tag ID's to add
                List <int> newPostTags = AdjTagIds.Except(OrgTagIds).ToList();

                if (deprecatedPostTags.Count > 0)
                {
                    foreach (int tagId in deprecatedPostTags)
                    {
                        PostTag postTag = new PostTag()
                        {
                            PostId = id,
                            TagId  = tagId
                        };
                        _tagRepository.DeletePostTag(postTag);
                    }
                    ;
                }
                if (newPostTags.Count > 0)
                {
                    foreach (int tagId in newPostTags)
                    {
                        PostTag postTag = new PostTag()
                        {
                            PostId = id,
                            TagId  = tagId
                        };
                        _tagRepository.AddPostTag(postTag);
                    }
                    ;
                }


                return(RedirectToAction("Details", "Post", new { id = vm.PostId }));
            }
            catch
            {
                return(View(vm));
            }
        }