public ActionResult Add(CustomerLocationAddViewModel viewModel) { var customerLocation = viewModel.CustomerLocation; customerLocation.CustomerId = viewModel.CustomerId; //Server side validation to make sure that the Address of the CustomerLocation doesn't exist //for this customer already. If so, throw a Model Error and return the Add Customer Location View. CustomerLocationValidator(customerLocation); if (ModelState.IsValid) { Context.CustomerLocations.Add(customerLocation); Context.SaveChanges(); TempData["Message"] = "A new location at " + customerLocation.Address + " has been added!"; return(RedirectToAction("Detail", "Customer", new { id = viewModel.CustomerId })); } //Reload the customer if there is a ModelState Error to display the Customer Name. viewModel.Customer = new GetCustomerQuery(Context) .Execute((int)viewModel.CustomerId, false); return(View(viewModel)); }
//Get the Add View. public ActionResult Add(int customerId) { //Load the Customer. var customer = new GetCustomerQuery(Context) .Execute(id: (int)customerId, includeRelatedEntities: false); if (customer == null) { return(HttpNotFound()); } //Assign the customer variable to the Customer Property of the CustomerLocationBaseViewModel // to display the Customer Name in the h2 of the Add View. var viewModel = new CustomerLocationAddViewModel(); { viewModel.Customer = customer; }; return(View(viewModel)); }