public ActionResult AdditionalDelete(int id) { var model = new AdditionalDeleteModel { Categories = _categoryService.GetAllCategories(0, -1, null).Select(x => x.ToModel()).ToList(), CategoryId = id }; model.Categories.First(m => m.Id == id).Name = "Un-assign"; return PartialView("_AdditionalDelete", model); }
public ActionResult Delete(int id, AdditionalDeleteModel deleteModel) { if (!_permissionService.Authorize(PermissionProvider.ManageCategories)) return AccessDeniedView(); var category = _categoryService.GetCategoryById(id); if (category == null) return RedirectToAction("index", new { page = Page, search = Search }); try { var projects = _projectService.GetAllProjectsByCategoryId(id); if(id == deleteModel.CategoryId) { foreach (var project in projects ) { project.Categories.Remove(project.Categories.First(c => c.Id == id)); } } else { foreach (var project in projects) { project.Categories.Remove(project.Categories.First(c => c.Id == id)); if (project.Categories.FirstOrDefault(c => c.Id == deleteModel.CategoryId) == null) project.Categories.Add(_categoryService.GetCategoryById(deleteModel.CategoryId)); } } _projectService.BulkUpdateProjects(projects); _categoryService.DeleteCategory(category); SuccessNotification(category.Name + " has been successfully deleted."); } catch (Exception) { ErrorNotification("An error occurred deleting " + category.Name + ", please try again"); } return RedirectToAction("index", new { page = Page, search = Search }); }