// GET: PostTagController/Create public ActionResult Create(int id) { var allTags = _tagRepository.GetAllTags(); var postTags = _postTagRepository.GetPostTagsByPostId(id); List <int> currentTagIds = new List <int>(); foreach (PostTag pt in postTags) { currentTagIds.Add(pt.TagId); } foreach (Tags t in allTags) { var tagsId = t.Id; if (currentTagIds.Contains(tagsId)) { t.Selected = true; } else { t.Selected = false; } } var post = _postRepository.GetPublishedPostById(id); var vm = new PostTagViewModel() { PostTagList = allTags, Post = post }; return(View(vm)); }