The problem is solved by the recursive method:
[HttpPost] public async Task<JsonResult> DeleteComment([FromBody] DeleteCommentViewModel obj) { if (ModelState.IsValid) { var comment = await _commentRepository.GetAll().SingleOrDefaultAsync(m => m.Id == obj.CommentId); if (comment != null) { await RemoveChildren(comment.Id); _commentRepository.Delete(comment); } if (Request.IsAjaxRequest()) { return Json(1); } } return Json(new { code = 0 }); } async Task RemoveChildren(int i) { var children = await _commentRepository.GetAll().Where(c => c.ParentId = i).ToListAsync(); foreach (var child in children) { await RemoveChildren(child.Id); _commentRepository.Delete(child); } }
Jahan source share