public ActionResult CreateCustomer()
        {
            var model = new CustomerModel();
            model.CustomerId = Guid.NewGuid();
            var billingAddress = new AddressModel { AddressId = Guid.NewGuid(), AddressTypeId = 0, CustomerId = model.CustomerId };
            var shippingAddress = new AddressModel { AddressId = Guid.NewGuid(), AddressTypeId = 1, CustomerId = model.CustomerId };
            model.AddressList = new List<AddressModel> {billingAddress, shippingAddress};

            return View(model);
        }
        public ActionResult CreateCustomer(CustomerModel customerModel)
        {
            var customer = new Customer();
            CustomerFromCustomerModel(customerModel, customer);
            foreach (var addressModel in customerModel.AddressList)
            {
                var a = new Address();
                AddressFromAddressModel(a, addressModel);
                customer.Addresses.Add(a);
            }

            var repository = new CustomerRepository();
            repository.CreateCustomer(customer);
            return RedirectToAction("Customers");
        }
        public ActionResult EditCustomer(CustomerModel customerModel)
        {
            var repository = new CustomerRepository();
            var customer = repository.GetCustomer(customerModel.CustomerId);
            CustomerFromCustomerModel(customerModel, customer);
            foreach (var addressModel in customerModel.AddressList)
            {
                var address = customer.Addresses.FirstOrDefault(a => a.AddressId == addressModel.AddressId);
                AddressFromAddressModel(address,addressModel);
            }

            repository.EditCustomer(customer);
            return RedirectToAction("Customers");
        }
        private CustomerModel CustomerModelFromCustomer(Customer customer)
        {
            var model = new CustomerModel
            {
                CustomerId = customer.CustomerId,
                FirstName = customer.FirstName,
                LastName = customer.LastName,
                MiddleI = customer.MiddleI
            };

            model.AddressList = new List<AddressModel>();

            foreach (var address in customer.Addresses.OrderBy(a=>a.AddressTypeId))
            {
                var addressModel = AddressModelFromAddress(address);
                model.AddressList.Add(addressModel);
            }

            return model;
        }
 private void CustomerFromCustomerModel(CustomerModel customerModel, Customer customer)
 {
     customer.CustomerId = customerModel.CustomerId;
     customer.FirstName = customerModel.FirstName;
     customer.LastName = customerModel.LastName;
     customer.MiddleI = customerModel.MiddleI;
 }