public ActionResult Edit([Bind(Include = "Comment_Id,Content")] Comment comment) { var logic = new HiddenLogic(); comment.Votes = 0; comment.EditingDate = DateTime.Now; comment.AddingDate = _comm.GetCommentWithAddingDate(comment); if (ModelState.IsValid) { _comm.UpdateContentAndPlusyAndEditDate(comment); _comm.SaveChanges(); //old tags: IList <CommentTag> listaCommentTagsActual = _comm.GetAllCommTagsByCommId(comment.Comment_Id); List <string> listOfTagNames = new List <string>(); foreach (var item in listaCommentTagsActual) { listOfTagNames.Add(_tag.GetTagNamesByTagID(item.Tag_Id)); } //list of name tags used //update of tags //new content here bool check = false; MatchCollection matches = Regex.Matches(comment.Content, @"\B(\#[a-zA-Z0-9-,_]+\b)"); //if comment has tags ->enter if (comment.CommentTags != null) { foreach (var tag in matches) { if (listOfTagNames.Any(p => p.Contains(tag.ToString().ToLower()))) { logic.CheckTheDifferenceBetween(matches.Count, listOfTagNames.Count); continue; } else { RemovecommentAndCommentTags(comment, tag); } } //if there isnt any tag used //bool check if after edtiting theres nothing relative to tags if (check == false && matches.Count != 0) { bool enterLoop = true; var listOfMatches = new List <Tag>(); foreach (var tagFake in matches) { var tag2 = _tag.GetTagByName(tagFake.ToString()); listOfMatches.Add(tag2); } //remove from checklist, not postTags (if you try to remove only 1 and you will keep the second --failure) foreach (var item in listaCommentTagsActual) { //get tagID by match and remove the rest //if matchesTags exists in listOfTagsActual - leave it; //new list of tags foreach (var tag in listOfMatches) { if (listOfMatches.Any(x => x.Tag_Id == item.Tag_Id)) { enterLoop = false; } else { enterLoop = true; } if (enterLoop == true) { if (_tag.CheckIfCommTagExist(item.Tag_Id, comment.Comment_Id)) { RemovecommentTag(item, comment.Comment_Id); } } enterLoop = true; } } } else if (check == false && matches.Count == 0) { foreach (var item in listaCommentTagsActual) { RemovecommentTag(item, comment.Comment_Id); } } } //if post is empty before and after is not else if (comment.CommentTags == null && matches != null) { foreach (var tag in matches) { RemovecommentAndCommentTags(comment, tag); } //if there isnt any tag used //bool check if after edtiting theres nothing relative to tags if (check == false) { foreach (var item in listaCommentTagsActual) { RemovecommentTag(item, comment.Comment_Id); } } } return(RedirectToAction("Index", "Post")); } return(View(comment)); }