public void Save(District obj)
 {
     if (obj.Id == 0)
         context.Entry(obj).State = System.Data.Entity.EntityState.Added;
     else
         context.Entry(obj).State = System.Data.Entity.EntityState.Modified;
     context.SaveChanges();
 }
        public ActionResult CreatePartial(District obj)
        {
            if(Request.IsAjaxRequest())
            {
                dataManager.Districts.Save(obj);

                return Json(new { Name = obj.Name, Id = obj.Id, Key = "DistrictId" }, JsonRequestBehavior.AllowGet);
            }
            return Json("", JsonRequestBehavior.AllowGet);
        }
 public ActionResult Create(District obj, FormCollection collection)
 {
     if (ModelState.IsValid)
     {
         if (!dataManager.Districts.GetAll().Any(o => o.Name == obj.Name))
         {
             dataManager.Districts.Save(obj);
             return RedirectToAction("Show", new { Id = obj.Id });
         }
         else
         {
             ModelState.AddModelError("Name",
                 "Район/город с названием \"" + obj.Name + "\" уже существует!");
             return View(obj);
         }
     }
     return View(obj);
 }
 public ActionResult Edit(District obj)
 {
     if (ModelState.IsValid)
     {
         if (!dataManager.Districts.GetAll()
             .Any(o =>
                 o.Name == obj.Name &&
                 o.Id != obj.Id))
         {
             var objFromDb = dataManager.Districts.Get(obj.Id);
             objFromDb.Name = obj.Name;
             objFromDb.ManagerId = obj.ManagerId;
             dataManager.Districts.Save(objFromDb);
             return RedirectToAction("Show", new { Id = obj.Id });
         }
         else
         {
             var managers = dataManager.Municipalities.GetAll().Where(m => m.ManagerId.HasValue).Select(m => m.ManagerId.Value).ToList();
             managers.AddRange(dataManager.Houses.GetAll().Where(h => h.ManagerId.HasValue).Select(h => h.ManagerId.Value).Distinct());
             managers.AddRange(dataManager.Districts.GetAll().Where(d => d.ManagerId.HasValue).Select(d => d.ManagerId.Value));
             managers.Remove((int?)obj.ManagerId ?? 0);
             ViewBag.Persons = from p in dataManager.Persons.GetAll().Where(p => !managers.Contains(p.Id))
                               select new SelectListItem
                               {
                                   Text = p.LastName + " " + p.FirstName + " " + p.MiddleName,
                                   Value = p.Id.ToString(),
                                   Selected = p.Id == obj.ManagerId
                               };
             ModelState.AddModelError("Name",
                 "Район/город с названием \"" + obj.Name + "\" уже существует!");
             return View(obj);
         }
     }
     return View(obj);
 }