public ActionResult New() { var membershipTypes = _context.MembershipTypes.ToList(); var viewModel = new CustomerFormViewModel { MembershipTypes = membershipTypes }; return View("CustomerForm", viewModel); }
public ActionResult Edit(int id) { var customer = _context.Customers.SingleOrDefault(c => c.Id == id); if (customer == null) { return HttpNotFound(); } var viewModel = new CustomerFormViewModel { Customer = customer, MembershipTypes = _context.MembershipTypes.ToList() }; return View("CustomerForm", viewModel); }
public ActionResult Save(Customer customer) { if (!ModelState.IsValid) { var viewModel = new CustomerFormViewModel { Customer = customer, MembershipTypes = _context.MembershipTypes.ToList() }; return View("CustomerForm", viewModel); } if (customer.Id == 0) { // create a new customer customer.MembershipType = _context.MembershipTypes.Find(customer.MembershipType.Id); _context.Customers.Add(customer); } else { var customerInDb = _context.Customers.Single(c => c.Id == customer.Id); customerInDb.Name = customer.Name; customerInDb.Birthdate = customer.Birthdate; customerInDb.IsSubscribedToNewsletter = customer.IsSubscribedToNewsletter; customerInDb.MembershipType = _context.MembershipTypes.Find(customer.MembershipType.Id); } try { _context.SaveChanges(); } catch (Exception ex) { return Content(ex.InnerException.ToString()); } return RedirectToAction("Index", "Customers"); }