public ActionResult Move(int nodeId, int? newParentId, bool list) { if (list) { return RedirectToAction("Index"); } if (nodeId == newParentId) { return RedirectToAction("Index"); } using (TreeContext context = new TreeContext()) { if (newParentId.HasValue && ContainsChilds(context, nodeId, newParentId.Value)) { return RedirectToAction("Index"); } var node = context.Folders.Where(x => x.Id == nodeId).Single(); node.ParentId = newParentId; context.SaveChanges(); } return RedirectToAction("Index"); }
public ActionResult Delete(int id) { using (TreeContext context = new TreeContext()) { DeleteNodes(context, id); context.SaveChanges(); } return RedirectToAction("Index"); }
public ActionResult Index() { using (TreeContext context = new TreeContext()) { FoldersListModel model = new FoldersListModel() { Folders = context.Folders.Where(x => !x.IsDeleted).ToArray().Select(x => new FoldersModel(x)) }; return View(model);//Возвращение список всех папок где параметр IsDeleted false } }
public ActionResult Add(int? parentId, string title, bool isList) { using (TreeContext context = new TreeContext()) { var newFolders = new Folders() { ParentId = parentId, Title = title, IsList=isList }; context.Folders.Add(newFolders); context.SaveChanges(); } return RedirectToAction("Index"); }
public NewsDBInitializer() { Database.SetInitializer<TreeContext>(null); try { using (var context = new TreeContext()) { if (!context.Database.Exists()) { ((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); } } } catch (Exception ex) { throw new InvalidOperationException("The news database could not be initialized.", ex); } }
//не совсем понятно private bool ContainsChilds(TreeContext context, int parentId, int id) { bool result = false; var inner = context.Folders.Where(x => x.ParentId == parentId && !x.IsDeleted).ToArray(); foreach (var node in inner) { if (node.Id == id && node.ParentId == parentId) { return true; } result = ContainsChilds(context, node.Id, id); } return result; }
//метод удаления записи private void DeleteNodes(TreeContext context, int id) { var inner = context.Folders.Where(x => x.ParentId == id && !x.IsDeleted).ToArray(); foreach (var node in inner) { node.IsDeleted = true; DeleteNodes(context, node.Id); } var deleted = context.Folders.Where(x => x.Id == id && !x.IsDeleted).Single(); deleted.IsDeleted = true; }