public ViewResult EditEstate(int? Id)
 {
     EstateModel modelEstate = new EstateModel();
     if (Id == null)
         modelEstate.estate = new Estates();
         
     else
         modelEstate.estate = repository.Estates.FirstOrDefault(x => x.id == Id);
     modelEstate.listUser = new SelectList(repository.Users.OrderBy(x => x.id), "Id", "Name");
     modelEstate.listRegion = new SelectList(repository.Regions.OrderBy(x => x.id), "Id", "Name");
     modelEstate.listCity = new SelectList(repository.Cities.OrderBy(x => x.id), "Id", "Name");
     return View(modelEstate);
 }
        public ActionResult EditEstate(EstateModel modelEstate)
        {

            modelEstate.estate.user_id = modelEstate.estate.Users.id;
            modelEstate.estate.region_id = modelEstate.estate.Regions.id;
            modelEstate.estate.city_id = modelEstate.estate.Cities.id;
            
            ValidateEstate(modelEstate.estate);
            
            if (ModelState.IsValid)
            {
                Estates dbEntry = repository.Estates.FirstOrDefault(x => x.id == modelEstate.estate.id);
                if (dbEntry != null)
                {
                    dbEntry.price = modelEstate.estate.price;
                    dbEntry.apt = modelEstate.estate.apt;
                    dbEntry.building = modelEstate.estate.building;
                    dbEntry.city_id = modelEstate.estate.city_id;
                    dbEntry.descr = modelEstate.estate.descr;
                    dbEntry.district_id = modelEstate.estate.district_id;
                    dbEntry.floor = modelEstate.estate.floor;
                    dbEntry.floors = modelEstate.estate.floors;
                    dbEntry.manager_id = modelEstate.estate.manager_id;
                    dbEntry.region_id = modelEstate.estate.region_id;
                    dbEntry.rooms = modelEstate.estate.rooms;
                    dbEntry.square = modelEstate.estate.square;
                    dbEntry.street = modelEstate.estate.street;
                    dbEntry.user_id = modelEstate.estate.user_id;
                    //dbEntry = modelEstate.estate;
                    repository.UpdateRecord<Estates>(modelEstate.estate);
                }
                else
                {
                    modelEstate.estate.Users = repository.Users.FirstOrDefault(x => x.id == modelEstate.estate.user_id);
                    modelEstate.estate.Regions = repository.Regions.FirstOrDefault(x => x.id == modelEstate.estate.region_id);
                    modelEstate.estate.Cities = repository.Cities.FirstOrDefault(x => x.id == modelEstate.estate.city_id);
                    repository.CreateRecord<Estates>(modelEstate.estate);
                    
                }
                return RedirectToAction("Table");
            }
            modelEstate.listUser = new SelectList(repository.Users.OrderBy(x => x.id), "Id", "Name");
            modelEstate.listRegion = new SelectList(repository.Regions.OrderBy(x => x.id), "Id", "Name");
            modelEstate.listCity = new SelectList(repository.Cities.OrderBy(x => x.id), "Id", "Name");
            return View(modelEstate);
        }