예제 #1
0
        public ActionResult Save(Customer customer)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new NewCustomerFormViewModel
                {
                    Customer        = customer,
                    MembershipTypes = _context.MembershipTypes.ToList()
                };

                return(View("CustomerForm", viewModel));
            }

            if (customer.Id == 0)
            {
                _context.Customers.Add(customer);
            }
            else
            {
                var customerInDb = _context.Customers.Single(c => c.Id == customer.Id);
                customerInDb.Name                 = customer.Name;
                customerInDb.BirthDate            = customer.BirthDate;
                customer.MembershipTypeId         = customer.MembershipTypeId;
                customer.IsSubscribedToNewsletter = customer.IsSubscribedToNewsletter;
            }

            _context.SaveChanges();
            return(RedirectToAction("Index", "Customers"));
        }
예제 #2
0
        public ActionResult Save(Customer customer)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new NewCustomerFormViewModel
                {
                    Customer = customer
                };

                return(View("CustomerForm", viewModel));
            }
            if (customer.Id == 0)
            {
                dbContext.Customers.Add(customer);
            }
            else
            {
                var cInDb = dbContext.Customers.Single(s => s.Id == customer.Id);
                cInDb.Name = customer.Name;
                cInDb.IsSubscribedToNewsletter = customer.IsSubscribedToNewsletter;
                cInDb.Birthdate = customer.Birthdate;
            }
            dbContext.SaveChanges();
            return(RedirectToAction("Index", "Customers"));
        }
        public ActionResult Save(Customer customer)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new NewCustomerFormViewModel()
                {
                    Customer        = customer,
                    MembershipTypes = _context.MembershipTypes.ToList(),
                    Address         = customer.Address
                };
            }

            if (customer.Id == 0)
            {
                _context.Customers.Add(customer);
                _context.Addresses.Add(customer.Address);
            }
            else
            {
                var updateCustomer = _context.Customers.Include(a => a.Address).Single(c => c.Id == customer.Id);
                var updateAddress  = _context.Addresses.SingleOrDefault(a => a.Id == updateCustomer.Address.Id);
                updateCustomer.FirstName        = customer.FirstName;
                updateCustomer.LastName         = customer.LastName;
                updateCustomer.DOB              = customer.DOB;
                updateCustomer.MembershipTypeId = customer.MembershipTypeId;
                updateAddress.AddressName       = customer.Address.AddressName;
            }
            _context.SaveChanges();
            return(RedirectToAction("Index", "Customers"));
        }
예제 #4
0
        public ActionResult New()
        {
            var viewModel = new NewCustomerFormViewModel
            {
                Customer = new Customer()
            };

            return(View("CustomerForm", viewModel));
        }
예제 #5
0
        public ActionResult New()
        {
            var membershipTypes = _context.MembershipTypes.ToList();
            var viewModel       = new NewCustomerFormViewModel
            {
                Customer        = new Customer(),
                MembershipTypes = membershipTypes
            };

            return(View("CustomerForm", viewModel));
        }
        public ActionResult NewCustomer()
        {
            var membershipTypes = _context.MembershipTypes.ToList();

            var customerViewModel = new NewCustomerFormViewModel
            {
                MembershipTypes = membershipTypes,
            };

            return(View("NewCustomerForm", customerViewModel));
        }
예제 #7
0
        public ActionResult Edit(int id)
        {
            var customer = dbContext.Customers.SingleOrDefault(c => c.Id == id);

            if (customer == null)
            {
                return(Content("Not found"));
            }

            var viewModel = new NewCustomerFormViewModel
            {
                Customer = customer
            };

            return(View("CustomerForm", viewModel));
        }
예제 #8
0
        public ActionResult Edit(int id)
        {
            var customer = _context.Customers.SingleOrDefault(c => c.Id == id);

            if (customer == null)
            {
                return(HttpNotFound());
            }

            var viewModel = new NewCustomerFormViewModel
            {
                Customer        = customer,
                MembershipTypes = _context.MembershipTypes.ToList()
            };

            return(View("CustomerForm", viewModel));
        }
예제 #9
0
        public ActionResult Save(Customer customer)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new NewCustomerFormViewModel
                {
                    Customer        = customer,
                    MembershipTypes = _context.MembershipTypes.ToList()
                };
                return(View("CustomerForm", viewModel));
            }


            if (customer.Id == 0)
            {
                _context.Customers.Add(customer);
            }
            else
            {
                var customerInDB = _context.Customers.Single(c => c.Id == customer.Id);

                //2 Methods to update
                //Opens up security holes in applications b/c it updates all properties
                //TryUpdateModel(customerInDB, "", new string[] { "Name", "Email" });

                //Mapper.Map(customer, customerInDb);

                customerInDB.Name                     = customer.Name;
                customerInDB.Birthdate                = customer.Birthdate;
                customerInDB.MembershipTypeId         = customer.MembershipTypeId;
                customerInDB.IsSubscribedtoNewsLetter = customer.IsSubscribedtoNewsLetter;
            }

            _context.SaveChanges();

            return(RedirectToAction("Index", "Customer"));
        }
예제 #10
0
        public ActionResult Save(Customer customer) //<-- Model Binding
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new NewCustomerFormViewModel
                {
                    Customer        = customer,
                    MembershipTypes = _context.MembershipTypes.ToList()
                };

                return(View("CustomerForm", viewModel));
            }

            if (customer.Id == 0)
            {
                _context.Customers.Add(customer); // Adds to memory only. DB context has a change tracking mechanism
            }
            else
            {
                var customerInDb = _context.Customers.Single(c => c.Id == customer.Id);
                customerInDb.Name                     = customer.Name;
                customerInDb.Birthdate                = customer.Birthdate;
                customerInDb.MembershipTypeId         = customer.MembershipTypeId;
                customerInDb.IsSubscribedToNewsletter = customer.IsSubscribedToNewsletter;

                //Can also use something like AutoMapper to update the properties. Look this up later.

                //    //The properties of this customer object will be updated based on the key/value pairs in the request data
                //    TryUpdateModel(customerInDb, new string[] { "Name", "Email" }); //Opens up security holes in the app
                //                     //Microsoft workaround ^^ Whitelist properties BUT now you have magic strings
            }

            _context.SaveChanges();                         // Persist the changes to DB

            return(RedirectToAction("Index", "Customers")); // Redirect User back to the list of customers
        }