public ActionResult DeletePost(Guid id) { int UserIDX = db_Accounts.GetUserIDX(); var post = db_Forum.GetPost_ByID(id); if (post != null) { //only allow delete if post by user or user is admin if (User.IsInRole("Admins") || UserIDX == post.MembershipUser_Id) { //if not topic starter, just delete post if (post.IsTopicStarter == false) { db_Forum.DeletePost(id); //update the topic to reflect correct "LastEdited Date" Post _lastPost = db_Forum.GetPost_LatestOfTopic(post.Topic_Id); if (_lastPost != null) { db_Forum.UpdateTopic_SetLastPostDate(post.Topic_Id, _lastPost.DateEdited); } //now sync with Azure AzureSearch.DeleteAzureGuid(id); TempData["Success"] = "Post deleted"; return(RedirectToAction("ShowTopic", "Forum", new { id = post.Topic_Id })); } else { //remove post id from topic db_Forum.OrphanTopic(post.Topic_Id); //delete all posts for the topic List <Post> _postList = db_Forum.GetPost_ByTopicID(post.Topic_Id); foreach (Post p in _postList) { db_Forum.DeletePost(p.Id); //now sync with Azure AzureSearch.DeleteAzureGuid(p.Id); } //then delete the topic int SuccID = db_Forum.DeleteTopic(post.Topic_Id); if (SuccID > 0) { //now sync with Azure //AzureSearch.DeleteAzureGuid(id); TempData["Success"] = "Topic deleted"; } return(RedirectToAction("Index", "Forum")); } } } TempData["Error"] = "Unable to delete"; return(RedirectToAction("LatestTopics", "Forum", new { slug = post.Topic_Id })); }