Exemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("ContractorId,Name,AddressId,IdentityUserId")] Client client, [Bind("Street,City,State,Zip")] Address address)
        {
            if (ModelState.IsValid)
            {
                var coords = await _geocodingService.GetCoords(address);

                address.Lat = coords[0];
                address.Lon = coords[1];

                _context.Addresses.Add(address);
                _context.SaveChanges();

                var userId = this.User.FindFirstValue(ClaimTypes.NameIdentifier);
                client.IdentityUserId = userId;
                client.Address        = address;
                client.AddressId      = address.AddressId;

                _context.Clients.Add(client);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(client));
        }