public ActionResult Save(Customer customer)
        {
            // ModelState not valid
            if (!ModelState.IsValid)
            {
                var vm = new CustomerFormVM
                {
                    Customer    = customer,
                    Memberships = _context.Memberships
                };

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

            // ModelState is valid
            if (customer.Id == 0)
            {
                _context.Customers.Add(customer);
            }
            else
            {
                var customerInDB = _context.Customers.FirstOrDefault(c => c.Id == customer.Id);
                customerInDB.Name      = customer.Name;
                customerInDB.Birthdate = customer.Birthdate;
                customerInDB.Address   = customer.Address;
                customerInDB.IsSubscribedToNewsLetter = customer.IsSubscribedToNewsLetter;
                customerInDB.MembershipId             = customer.MembershipId;
            }

            _context.SaveChanges();

            return(RedirectToAction("Index", "Customer"));
        }
        public ActionResult Save(Customer customer)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new CustomerFormVM
                {
                    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;
                customerInDb.MembershipTypeId         = customer.MembershipTypeId;
                customerInDb.IsSubscribedToNewsLetter = customer.IsSubscribedToNewsLetter;
                customerInDb.Phone                    = customer.Phone;
                customerInDb.Address                  = customer.Address;
            }
            _context.SaveChanges();

            return(RedirectToAction("Index", "Customers"));
        }
Beispiel #3
0
        public ActionResult CustomerForm()
        {
            var membershipTypes = _context.MembershipTypes.ToList();
            var viewModel       = new CustomerFormVM
            {
                Customer        = new Customer(),
                MembershipTypes = membershipTypes
            };

            return(View(viewModel));
        }
        // GET Customer/New
        public ActionResult New()
        {
            var memberships = _context.Memberships;

            var vm = new CustomerFormVM
            {
                Customer    = new Customer(),
                Memberships = memberships
            };

            return(View("CustomerForm", vm));
        }
        public ActionResult Edit(int id)
        {
            var customer = _context.Customers.SingleOrDefault(c => c.Id == id);

            if (customer == null)
            {
                return(HttpNotFound());
            }
            var viewModel = new CustomerFormVM()
            {
                Customer        = customer,
                MembershipTypes = _context.MembershipTypes.ToList()
            };

            return(View("CustomerForm", viewModel));
        }
        // GET: Customer/Edit/2
        public ActionResult Edit(int id)
        {
            var customer = _context.Customers.FirstOrDefault(c => c.Id == id);

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

            var memberships = _context.Memberships;

            var vm = new CustomerFormVM
            {
                Customer    = customer,
                Memberships = memberships
            };

            return(View("CustomerForm", vm));
        }
Beispiel #7
0
        public ActionResult Save(Customer customer)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new CustomerFormVM
                {
                    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;
                customerInDb.MembershipTypeId         = customer.MembershipTypeId;
                customerInDb.IsSubscribedToNewsLetter = customer.IsSubscribedToNewsLetter;
            }

            try
            {
                _context.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                Console.WriteLine(e);
            }

            return(RedirectToAction("Index", "Customer"));
        }