public ActionResult Create([Bind(Include = "Id,Name")] Tree tree, int?id) { if (ModelState.IsValid) { db.Trees.Add(tree); db.SaveChanges(); if (id != null) { Tree parent = db.Trees.SingleOrDefault(t => t.Id == id); if (parent != null) { tree.Parent = parent; } else { tree.Parent = tree; } } else { tree.Parent = tree; } db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ParentId = new SelectList(db.Trees, "Id", "Name", tree.ParentId); return(View(tree)); }
public async Task <VMGetNodes> CreateAsync(VMAddNodes node) { if (!_validator.Validate(node.Name)) { throw new ArgumentException("Data not valid. Name must has maximum 30 chars."); } var level = _context.Nodes.Where(x => x.IdNode == node.ParentId).Select(x => x.Level).FirstOrDefault(); var newNode = new DbNode { Level = level + 1, Name = node.Name, ParentId = node.ParentId }; var result = await _context.Nodes.AddAsync(newNode); _context.SaveChanges(); var dbNode = result.Entity; var viewModel = new VMGetNodes(); viewModel.IdNode = dbNode.IdNode; viewModel.Level = dbNode.Level; viewModel.Name = dbNode.Name; viewModel.ParentId = dbNode.ParentId; return(viewModel); }
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 int Create(EasyTree entity) { using (TreeContext db = new TreeContext()) { db.Set <EasyTree>().Add(entity); return(db.SaveChanges()); } }
public int Edit(EasyTree entity) { using (TreeContext db = new TreeContext()) { db.Set <EasyTree>().Attach(entity); db.Entry <EasyTree>(entity).State = EntityState.Modified; return(db.SaveChanges()); } }
public ActionResult Delete(int id)//метод принимающий параметры для удаления записи { using (TreeContext context = new TreeContext()) { DeleteNodes(context, id); context.SaveChanges(); } return(RedirectToAction("Index")); }
public int Delete(int id) { using (TreeContext db = new TreeContext()) { EasyTree entity = db.EasyTrees.SingleOrDefault(a => a.id == id); db.Set <EasyTree>().Remove(entity); int row = db.SaveChanges(); return(row > 0 ? 1 : 0); } }
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 bool SaveChanges() { _context.SaveChanges(); return(true); }