public ActionResult DeleteConfirmed(int? id)
        {
            if (id == null)
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            TreeViewModel db_cs = new TreeViewModel();
            CompanyEntity comp = db_cs.CompaniesTreeViewTable.Find(id);
            if (comp == null)
                return HttpNotFound();

            //get childs for change it's parent
            IQueryable<CompanyEntity> childs = db_cs.CompaniesTreeViewTable.Where(c => c.ParentId == comp.Id);
            if (childs.Count() > 0)
                //move childs to up level in tree
                foreach (CompanyEntity c_c in childs)
                {
                    c_c.ParentId = comp.ParentId;
                    db_cs.Entry(c_c).State = EntityState.Modified;
                }

            db_cs.CompaniesTreeViewTable.Remove(comp);

            db_cs.SaveChanges();
            return RedirectToAction("GetTree");
        }
 //Load from data base
 public CompaniesTree(TreeViewModel db)
 {
     //find all companies for tree root
     rootNodes = new List<CompanyNode>();
     List<CompanyEntity> cps = db.CompaniesTreeViewTable.Where(s => s.ParentId == 0).ToList();
     foreach (CompanyEntity c in cps)
         rootNodes.Add(new CompanyNode(null, c, db));
 }
 public ActionResult DeleteCompany(int? id)
 {
     if (id == null)
         return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
     int _id = (int)id;
     TreeViewModel db_cs = new TreeViewModel();
     CompanyEntity company = db_cs.CompaniesTreeViewTable.Find(_id);
     if (company == null)
         return HttpNotFound();
     return View(company);
 }
        public ActionResult CreateCompany(
           [Bind(Include = "Id, Name,Earning,ParentId")] CompanyEntity comp)
        {
            if (ModelState.IsValid)
            {
                TreeViewModel db_cs = new TreeViewModel();
                db_cs.CompaniesTreeViewTable.Add(comp);
                db_cs.SaveChanges();

            }
            return View(comp);
        }
        public CompanyNode(CompanyNode parent, CompanyEntity company, TreeViewModel db)
        {
            this.parent = parent;
            this.company = company;
            if (this.company.Earning == null)
                totallEarning = 0;
            else
                totallEarning = (int)this.company.Earning;

            List<CompanyEntity> childCompanies = db.CompaniesTreeViewTable.Where(c => c.ParentId == company.Id).OrderBy(c => c.Name).ToList();
            foreach (CompanyEntity c in childCompanies)
                AddChild(new CompanyNode(this, c, db));
        }
        public ActionResult EditCompany(
            [Bind(Include = "Id, Name,Earning,ParentId")] CompanyEntity comp)
        {
            if (ModelState.IsValid)
            {
                TreeViewModel db_cs = new TreeViewModel();
                db_cs.Entry(comp).State = EntityState.Modified;
                db_cs.SaveChanges();
            }

            return View(comp);
        }