public static CustomerEditModel Create(Customer c)
        {
            var item = new CustomerEditModel()
            {
                EditId             = c.Id,
                Gender             = c.Gender.HasValue ? (int)c.Gender : (-1),
                CityId             = c.CityId,
                CityName           = c.City?.Name ?? string.Empty,
                ContractSum        = c.ContractSum,
                FirstName          = c.FirstName,
                MidleName          = c.MidleName,
                LastName           = c.LastName,
                IsArchive          = c.IsArchive,
                IsSiteAccess       = c.IsSiteAccess,
                MinSum             = c.MinSum,
                MaxSum             = c.MaxSum,
                ReheshSum          = c.ReheshSum,
                DateMeeting        = c.DateMeeting,
                Status             = (int)c.Status,
                Phone1             = c.Phones.SingleOrDefault(x => x.Order == 0)?.Number,
                Phone2             = c.Phones.SingleOrDefault(x => x.Order == 1)?.Number,
                Phone3             = c.Phones.SingleOrDefault(x => x.Order == 2)?.Number,
                DistrictList       = c.DistrictToClients.Select(x => x.DistrictId).ToList(),
                HousingTypeListIds = c.TypesHousingToCustomers.Select(x => x.TypesHousingId).ToList()
            };

            return(item);
        }
 public static CustomerEditModel Create(Customer c)
 {
     var item = new CustomerEditModel()
     {
         EditId = c.Id,
         Gender = c.Gender.HasValue ? (int)c.Gender : (-1),
         CityId = c.CityId,
         CityName = c.City?.Name ?? string.Empty,
         ContractSum = c.ContractSum,
         FirstName = c.FirstName,
         MidleName = c.MidleName,
         LastName = c.LastName,
         IsArchive = c.IsArchive,
         IsSiteAccess = c.IsSiteAccess,
         MinSum = c.MinSum,
         MaxSum = c.MaxSum,
         ReheshSum = c.ReheshSum,
         DateMeeting = c.DateMeeting,
         Status = (int)c.Status,
         Phone1 = c.Phones.SingleOrDefault(x => x.Order == 0)?.Number,
         Phone2 = c.Phones.SingleOrDefault(x => x.Order == 1)?.Number,
         Phone3 = c.Phones.SingleOrDefault(x => x.Order == 2)?.Number,
         DistrictList = c.DistrictToClients.Select(x => x.DistrictId).ToList(),
         HousingTypeListIds = c.TypesHousingToCustomers.Select(x => x.TypesHousingId).ToList()
     };
     
     return item;
 }
        public IActionResult Create(CustomerEditModel model)
        {
            if (ModelState.IsValid)
            {
                var customer = new Customer();
                model.UpdateEntity(customer);
                _context.Clients.Add(customer);
                _context.SaveChanges();

                model.UpdateDistricts(customer);
                model.UpdateHousingTypes(customer);
                _context.SaveChanges();

                return RedirectToAction("Index");
            }
           
            return View("Save",model);
        }
        public IActionResult Edit(CustomerEditModel model)
        {
            if (ModelState.IsValid)
            {
                var customer = _context.Clients
                    .Include(x => x.DistrictToClients)
                    .Include(x => x.TypesHousingToCustomers)
                    .Include(x => x.Phones)
                    .Single(x => x.Id == model.EditId);

                model.UpdateEntity(customer);
                model.UpdateDistricts(customer);
                model.UpdateHousingTypes(customer);
                _context.Update(customer);
                
                _context.SaveChanges();
                return RedirectToAction("Index");
            }
            return View("Save", model);
        }
 public IActionResult Create()
 {
     var model = new CustomerEditModel();
     return View("Save", model);
 }