public async Task <IActionResult> Edit(int id, [Bind("Id,Line1,Line2,ZipCode,Coordinates,CityId")] Address address)
        {
            if (id != address.Id)
            {
                return(NotFound());
            }

            ///Serve para habilitar as validações se o JavScript tiver desabilitado no navegador do user
            if (!ModelState.IsValid)
            {
                var cities = await _cityService.FindAllAsync();

                var viewModel = new AddressFormViewModel {
                    Address = address, Citys = cities
                };
                return(View(viewModel));
            }

            if (id != address.Id)
            {
                return(RedirectToAction(nameof(Error), new { message = "Id mismatch." }));
            }
            try
            {
                await _addressService.UpdateAsync(address);
            }
            catch (ApplicationException e)
            {
                return(RedirectToAction(nameof(Error), new { message = e.Message }));
            }

            return(RedirectToAction(nameof(Index)));
        }
        // GET: IMS/Address/Save
        public ActionResult Save(AddressFormViewModel viewModel)
        {
            if (viewModel.Address.Id != 0)
            {
                var addressInDb = _context.Address.Single(a => a.Id == viewModel.Address.Id);

                addressInDb.Name         = viewModel.Address.Name;
                addressInDb.AddressLine1 = viewModel.Address.AddressLine1;
                addressInDb.AddressLine2 = viewModel.Address.AddressLine2;
                addressInDb.AddressLine3 = viewModel.Address.AddressLine3;
                addressInDb.City         = viewModel.Address.City;
                addressInDb.County       = viewModel.Address.County;
                addressInDb.PostCode     = viewModel.Address.PostCode;
                addressInDb.Country      = viewModel.Address.Country;
            }
            else
            {
                var customerInDb = _context.Customers.Single(c => c.Id == viewModel.CustomerId);
                customerInDb.SavedAddresses.Add(viewModel.Address);
            }

            _context.SaveChanges();

            return(RedirectToAction("Addresses", "Customers", new { id = viewModel.CustomerId }));
        }
        // GET: Addresses/Create
        public async Task <IActionResult> Create()
        {
            var citys = await _cityService.FindAllAsync();

            var viewModel = new AddressFormViewModel {
                Citys = citys
            };

            return(View(viewModel));
        }
        public ActionResult Edit(int customerId, int id)
        {
            var addressInDb = _context.Address.SingleOrDefault(a => a.Id == id);

            var viewModel = new AddressFormViewModel
            {
                CustomerId = customerId,
                Address    = addressInDb,
            };

            return(View("AddressForm", viewModel));
        }
        // GET: IMS/Address/New
        public ActionResult New(int?customerId)
        {
            var viewModel = new AddressFormViewModel
            {
                Address = new Address(),
            };

            if (customerId != null)
            {
                viewModel.CustomerId = (int)customerId;
            }

            return(View("AddressForm", viewModel));
        }
        public async Task <IActionResult> Create(Address address)
        {
            if (!ModelState.IsValid)
            {
                var citys = await _cityService.FindAllAsync();

                var viewModel = new AddressFormViewModel {
                    Address = address, Citys = citys
                };
                return(View(viewModel));
            }
            await _addressService.InsertAsync(address);

            return(RedirectToAction(nameof(Index)));
        }
        // GET: Addresses/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var address = await _addressService.FindByIdAsync(id);

            if (address == null)
            {
                return(NotFound());
            }
            List <City> Citys = await _cityService.FindAllAsync();

            AddressFormViewModel viewModel = new AddressFormViewModel {
                Address = address, Citys = Citys
            };

            return(View(viewModel));
        }