public ActionResult Create(Municipality obj)
 {
     if (ModelState.IsValid)
     {
         if (!dataManager.Municipalities.GetAll()
             .Where(m => m.DistrictId == obj.DistrictId)
             .Any(o => o.Name == obj.Name))
         {
             dataManager.Municipalities.Save(obj);
             return RedirectToAction("Show", new { Id = obj.Id });
         }
         else
             ModelState.AddModelError("Name",
                 "МТУ с названием \"" + obj.Name + "\" уже существует!");
     }
     ViewBag.Districts = from d in dataManager.Districts.GetAll()
                         select new SelectListItem
                         {
                             Text = d.Name,
                             Value = d.Id.ToString(),
                             Selected = obj.DistrictId.HasValue ? obj.DistrictId.Value == d.Id : false
                         };
     return View(obj);
 }
 public ActionResult Edit(Municipality obj)
 {
     if (ModelState.IsValid)
     {
         if (!dataManager.Municipalities.GetAll()
             .Where(m => m.DistrictId == obj.DistrictId)
             .Any(o => o.Id != obj.Id && o.Name == obj.Name))
         {
             var objFromDb = dataManager.Municipalities.Get(obj.Id);
             objFromDb.Name = obj.Name;
             objFromDb.DistrictId = obj.DistrictId;
             objFromDb.ManagerId = obj.ManagerId;
             dataManager.Municipalities.Save(objFromDb);
             return RedirectToAction("Show", new { Id = obj.Id });
         }
         else
         {
             ViewBag.Districts = from d in dataManager.Districts.GetAll()
                                 select new SelectListItem
                                 {
                                     Text = d.Name,
                                     Value = d.Id.ToString(),
                                     Selected = obj.DistrictId == d.Id
                                 };
             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 !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);
 }