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")); }
public IHttpActionResult AddCustomer(CustomerDTO customerDto) { if (!ModelState.IsValid) { return(BadRequest()); //throw new HttpResponseException(HttpStatusCode.BadRequest); } var customer = Mapper.Map <CustomerDTO, Customer>(customerDto); var addCustomer = _context.Customers.Add(customer); _context.SaveChanges(); customerDto.Id = customer.Id; // return customerDto; return(Created(new Uri(Request.RequestUri + "/" + customer.Id), customerDto)); }
public IHttpActionResult Delete(int id) { var book = _context.Books.SingleOrDefault(b => b.Id == id); if (book == null) { return(NotFound()); } _context.Books.Remove(book); _context.SaveChanges(); return(Ok("Deleted")); }
public ActionResult Save(Book book) { if (!ModelState.IsValid) { var viewModel = new AddBookViewModel { Book = book, BookCategories = _context.BookCategories.ToList() }; return(View("AddBookForm", viewModel)); } if (book.Id == 0) { _context.Books.Add(book); } else { var updateBookInfo = _context.Books.Single(b => b.Id == book.Id); updateBookInfo.Author = book.Author; updateBookInfo.ISBN = book.ISBN; updateBookInfo.Name = book.Name; updateBookInfo.Publisher = book.Publisher; updateBookInfo.YearPublished = book.YearPublished; updateBookInfo.BookCategoryId = book.BookCategoryId; } try { _context.SaveChanges(); } catch (DbEntityValidationException e) { Console.WriteLine(e); } return(RedirectToAction("Add", "Books")); }