public ActionResult SavePostTags(int postId, TreeNodeCollection treeNodeCollection, UnitOfWork uow) { ActionResult result = new ActionResult(); try { var thisPostTags = new TagRepository().GetAllTagsForPost(postId); var allTags = new TagRepository().GetAll(); foreach (TreeNode node in treeNodeCollection) { var tag = allTags.SingleOrDefault(a => a.Name == node.Text); TagRepository.TagHelper relatedItem = null; if (thisPostTags.Any()) { relatedItem = thisPostTags.SingleOrDefault(a => a.PostId == postId && a.TagId == tag.Id); } if (node.Checked) { if (relatedItem == null) { uow.TagPosts.Create(new TagPost() { PostId = postId, TagId = tag.Id }); } } else { if (thisPostTags.Any()) { var tobeDeleted = thisPostTags.SingleOrDefault(a => a.PostId == postId && a.TagId == tag.Id); if (relatedItem != null) { uow.TagPosts.Delete(a => a.TagId == tobeDeleted.TagId && a.PostId == tobeDeleted.PostId); } } } } result.IsSuccess = true; } catch (LocalException exception) { //result.IsSuccess = false; //result.Message = exception.ResultMessage; //Debuging.Warning(exception, "SavePostTags Method"); lblResult.Text = exception.Message; } catch (Exception exception) { //result.IsSuccess = false; //result.Message = MessageText.UNKNOWN_ERROR; //Debuging.Error(exception, "SavePostTags Method"); lblResult.Text = exception.Message; } return(result); }