//Get the Add view public ActionResult Add(int customerLocationId) { //Load the CustomerLocation var customerLocation = new GetCustomerLocationQuery(Context) .Execute(id: (int)customerLocationId, includeCustomer: false); if (customerLocation == null) { return(HttpNotFound()); } //Set the CustomerLocation and CustomerId properties in the ViewModel var viewModel = new ServiceCustomerLocationAddViewModel() { CustomerLocation = customerLocation, CustomerId = customerLocation.CustomerId }; //Load the SelectList of Services. viewModel.Initialize(Context); return(View(viewModel)); }
public ActionResult Add(ServiceCustomerLocationAddViewModel viewModel) { //Server side validation to make sure the ServiceCustomerLocation does not already exist. If not, //a ModelState error will be thrown. ServiceCustomerLocationValidator(viewModel); //Load the Customer Location so that the CustomerLocation Address can be passed //to the TempData message or back to the ViewModel if a ModelState Error is thrown. viewModel.CustomerLocation = new GetCustomerLocationQuery(Context) .Execute(id: (int)viewModel.CustomerLocationId, includeCustomer: false); if (ModelState.IsValid) { //Set the ServiceId and CustomerLocationID in a new ServiceCustomerLocation var serviceCustomerLocation = new ServiceCustomerLocation() { CustomerLocationId = viewModel.CustomerLocationId, ServiceId = viewModel.ServiceId }; Context.ServiceCustomerLocations.Add(serviceCustomerLocation); Context.SaveChanges(); //Load the ServiceName using the ServiceId that was selected by the user so that it can be passed //to the TempData messsage. var serviceAdded = Context.Services .Where(s => s.ServiceId == serviceCustomerLocation.ServiceId) .Select(s => s.ServiceName) .SingleOrDefault(); TempData["Message"] = serviceAdded + " was added to " + viewModel.CustomerLocation.Address + "!"; return(RedirectToAction("Detail", "Customer", new { id = viewModel.CustomerId })); } //If there is a ModelState error, reload the SelectList of Services. viewModel.Initialize(Context); return(View(viewModel)); }