// GET: PostTagController/Create public ActionResult Create(int id) { var allTags = _tagRepository.GetAllTags(); //get all relationships belonging to this post (PostTag) var postTags = _postTagRepo.GetPostTagsByPostId(id); List <int> foundTagIds = new List <int>(); //1. take the TagId's from each PostTag and add to list of id ints = has rel to this Post foreach (PostTag pt in postTags) { foundTagIds.Add(pt.TagId); } // loop through allTags, if Id is present in list 1 assign boolean true, if not assign boolean false (contains) foreach (Tag t in allTags) { var tId = t.Id; if (foundTagIds.Contains(tId)) { t.Selected = true; } else { t.Selected = false; } } var post = _postRepo.GetPublishedPostById(id); var vm = new TagIndexViewModel() { TagsOnPost = allTags, Post = post, }; return(View(vm)); }
public IActionResult GetPostTagsByPostId(int id) { return(Ok(_postTagRepository.GetPostTagsByPostId(id))); }