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));
        }
Beispiel #2
0
        protected override void Seed(LawnPaynesContext context)
        {
            var customer1 = new Customer()
            {
                CustomerId  = 1,
                Name        = "John Doe",
                IsNew       = false,
                PhoneNumber = "(345) 678-1234",
                Email       = "*****@*****.**",
                Comments    = "Test a comment!"
            };


            var customer2 = new Customer()
            {
                CustomerId  = 2,
                Name        = "Jane Doe",
                IsNew       = true,
                PhoneNumber = "(800) 123-4567",
                Email       = "*****@*****.**",
                Comments    = "Test a comment again!"
            };

            var customer3 = new Customer()
            {
                CustomerId  = 3,
                Name        = "Ryan Sallee",
                IsNew       = false,
                PhoneNumber = "(502) 111-2222",
                Email       = "*****@*****.**",
                Comments    = "Visit weekly."
            };

            context.Customers.AddOrUpdate(customer1, customer2, customer3);

            var customerLocation1 = new CustomerLocation()
            {
                CustomerLocationId = 1,
                Address            = "1333 Jones St, Louisville, KY 40218",
                CustomerId         = 1
            };

            var customerLocation2 = new CustomerLocation()
            {
                CustomerLocationId = 2,
                Address            = "5200 Indian Trl, Louisville, KY 40218",
                CustomerId         = 3
            };

            context.CustomerLocations.AddOrUpdate(customerLocation1, customerLocation2);

            var service1 = new Service()
            {
                ServiceId   = 1,
                ServiceName = "Cut"
            };

            var service2 = new Service()
            {
                ServiceId   = 2,
                ServiceName = "Trim"
            };

            var service3 = new Service()
            {
                ServiceId   = 3,
                ServiceName = "Landscaping"
            };

            context.Services.AddOrUpdate(service1, service2, service3);

            var serviceCustomerLocation1 = new ServiceCustomerLocation()
            {
                CustomerLocationId = 2,
                ServiceId          = 1
            };

            var serviceCustomerLocation2 = new ServiceCustomerLocation()
            {
                CustomerLocationId = 2,
                ServiceId          = 2
            };

            var serviceCustomerLocation3 = new ServiceCustomerLocation()
            {
                CustomerLocationId = 1,
                ServiceId          = 2
            };

            context.ServiceCustomerLocations.AddOrUpdate(serviceCustomerLocation1, serviceCustomerLocation2, serviceCustomerLocation3);
        }