public ActionResult Create(DistribucionTerritorial dt)
        {
            DistribucionTerritorial dtPadre = null;

            if (dt.PadreId != null)
            {
                dtPadre = db.DistribucionesTerritorial.Where(x => x.Id == dt.PadreId).ToList().FirstOrDefault();
            }

            if (dt.Distintivo == null)
            {
                dt.Distintivo = dt.Nombre;
            }

            if (dt.PadreId != null)
            {
                dt.Nivel  = dtPadre.Nivel + 1;
                dt.Parent = dtPadre;
            }
            else
            {
                dt.Nivel = 0;
            }
            dt.FechaIngreso = DateTime.Now;
            db.DistribucionesTerritorial.Add(dt);

            dt.Id = db.SaveChanges();

            ViewBag.Geography = LoadGeograpy();


            return(View());
        }
        public ActionResult Edit(long Id)
        {
            DistribucionTerritorial dt = db.DistribucionesTerritorial.Where(x => x.Id == Id).ToList().FirstOrDefault();

            ViewBag.Geography = LoadGeograpy();

            return(View(dt));
        }
        public ActionResult Delete(long id)
        {
            DistribucionTerritorial dt = db.DistribucionesTerritorial.Find(id);

            db.DistribucionesTerritorial.Remove(dt);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
        public ActionResult Create(long padreId = 0)
        {
            ViewBag.Geography = LoadGeograpy();

            if (padreId != 0)
            {
                DistribucionTerritorial dtPadre = null;
                DistribucionTerritorial dt      = null;

                dtPadre = db.DistribucionesTerritorial.Where(x => x.Id == padreId).ToList().FirstOrDefault();
                dt      = new DistribucionTerritorial
                {
                    PadreId = dtPadre.Id,
                    Nivel   = dtPadre.Nivel + 1,
                    Parent  = dtPadre
                };
                return(View(dt));
            }

            return(View());
        }
        public ActionResult Edit(DistribucionTerritorial dt)
        {
            DistribucionTerritorial dt2 = db.DistribucionesTerritorial.Where(x => x.Id == dt.Id).ToList().FirstOrDefault();

            dt2.Nombre     = dt.Nombre;
            dt2.Distintivo = dt.Distintivo;
            dt2.PadreId    = dt.PadreId;
            dt2.Nivel      = dt.Nivel;
            dt2.FechaUltimaModificacion = DateTime.Now;
            dt2.Tipo = dt.Tipo;

            if (dt.PadreId != null)
            {
                DistribucionTerritorial dtPadre = db.DistribucionesTerritorial.Where(x => x.Id == dt.PadreId).ToList().FirstOrDefault();
                dt2.Parent = dtPadre;
            }


            db.SaveChanges();

            ViewBag.Geography = LoadGeograpy();

            return(View(dt2));
        }