示例#1
0
        public ActionResult Save(Customer customer) //model binding (MVC bind this model to the request data)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new CustomerEditFormViewModel
                {
                    Customer        = customer,
                    MembershipTypes = _context.MembershipTypes.ToList()
                };
                return(View("CustomerEditForm", viewModel));
            }

            if (customer.Id == 0)
            {
                _context.Customers.Add(customer);
            }

            else
            {
                var customerInDb = _context.Customers.Single(c => c.Id == customer.Id);

                //TryUpdateModel(customerInDb);

                customerInDb.Name                     = customer.Name;
                customerInDb.DateOfBirth              = customer.DateOfBirth;
                customerInDb.MembershipTypeId         = customer.MembershipTypeId;
                customerInDb.IsSubscribedToNewsletter = customer.IsSubscribedToNewsletter;
            }
            _context.SaveChanges();

            return(RedirectToAction("Index", "Customers")); //zamiast NewCustomerViewModel moge Customer customer i MVC jest smart enough :) (Property sa z prefix Customer. )
        }
示例#2
0
        public ActionResult New() //Custromers/New
        {
            //do dropdown potrzebuje liste membershipTypes z bazy. (najpierw DbSet)

            var membershipTypes = _context.MembershipTypes.ToList();

            var viewModel = new CustomerEditFormViewModel()
            {
                MembershipTypes = membershipTypes
            };

            return(View("CustomerEditForm", viewModel));
            //membershipTypes lepiej przez ViewModel bo pozniej potrzebuje przeslac customersow w razie implementacji edycji.
        }
示例#3
0
        public ActionResult Edit(int id)  //id bo w view index w action link.
        {
            var customer = _context.Customers.SingleOrDefault(c => c.Id == id);

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

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

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