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) _context.Customers.Add(customer); else { var customerInDb = _context.Customers.Single(c => c.Id == customer.Id); //Mapper.Map(customer, customerInDb); customerInDb.MembershipTypeId = customer.MembershipTypeId; customerInDb.Name = customer.Name; customerInDb.Birthday = customer.Birthday; customerInDb.IsSubscribedToNewsletter = customer.IsSubscribedToNewsletter; customerInDb.MembershipType = customer.MembershipType; } _context.SaveChanges(); return RedirectToAction("GetCustomers"); }
public Customer CreateCustomer(Customer customer) { if(!ModelState.IsValid) throw new HttpResponseException(HttpStatusCode.BadRequest); _context.Customers.Add(customer); _context.SaveChanges(); return customer; }
public ActionResult Save(Customer customer) { 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; } _context.SaveChanges(); return RedirectToAction("Index", "Customers"); }
public void UpdateCustomer(int id, Customer customer) { if (!ModelState.IsValid) throw new HttpResponseException(HttpStatusCode.BadRequest); var customerInDb = _context.Customers.SingleOrDefault(c => c.Id == id); if(customerInDb == null) throw new HttpResponseException(HttpStatusCode.NotFound); customerInDb.Name = customer.Name; customerInDb.Birthdate = customer.Birthdate; customerInDb.IsSubscribedToNewsletter = customer.IsSubscribedToNewsletter; customerInDb.MembershipTypeId = customer.MembershipTypeId; _context.SaveChanges(); }