public async Task <IActionResult> Edit(int id, [Bind("Director,Released,ProductId,Title,Price,ImageUrl,Category,CreatedDate")] Movie movie) { if (id != movie.ProductId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(movie); _context.Entry <Movie>(movie).Property(x => x.CreatedDate).IsModified = false; await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!MovieExists(movie.ProductId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(movie)); }
public async Task <IActionResult> Edit(int id, [Bind("Author,Publisher,Published,ISBN,ProductId,Title,Price,ImageUrl,Category,CreatedDate")] Book book) { if (id != book.ProductId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(book); _context.Entry <Book>(book).Property(x => x.CreatedDate).IsModified = false; await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BookExists(book.ProductId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(book)); }
public async Task <IActionResult> Edit(int id, [Bind("Artist,Label,Released,ProductId,Title,Price,ImageUrl,Category")] MusicCD musicCD) { if (id != musicCD.ProductId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(musicCD); _context.Entry <MusicCD>(musicCD).Property(x => x.CreatedDate).IsModified = false; await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!MusicCDExists(musicCD.ProductId)) { return(NotFound()); } else { ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); } } return(RedirectToAction(nameof(Index))); } return(View(musicCD)); }
public async Task <IActionResult> PutCustomer(int id, Customer customer) { if (id != customer.CustomerId) { return(BadRequest()); } _context.Entry(customer).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CustomerExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public ActionResult Edit([Bind(Include = "ProductId,Title,Price,ImageUrl,Category,Director")] Movie movie) { try { if (ModelState.IsValid) { db.Entry(movie).State = EntityState.Modified; db.Entry(movie).Property(c => c.CreatedDate).IsModified = false; db.SaveChanges(); return(RedirectToAction("Index")); } } catch (DataException /* dex */) { //Log the error (uncomment dex variable name and add a line here to write a log. ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); } return(View(movie)); }
public async Task <IActionResult> Checkout(Order order) { if (cart.Lines.Count == 0) { ModelState.AddModelError("", "Sorry, your cart is empty!"); } if (ModelState.IsValid) { // order processing logic Customer customer = new Customer { Firstname = order.FirstName, Lastname = order.LastName, Address = order.Line1 + " " + order.Line2 + " " + order.Line3, City = order.City, Zip = order.Zip, Email = order.Email }; if (dataContext.Customers.Any(c => c.Firstname == customer.Firstname && c.Lastname == customer.Lastname && c.Email == customer.Email)) { customer = dataContext.Customers.Where(c => c.Firstname == customer.Firstname && c.Lastname == customer.Lastname && c.Email == customer.Email).First(); customer.Address = order.Line1 + " " + order.Line2 + " " + order.Line3; customer.Zip = order.Zip; // ensure update instead of insert dataContext.Entry(customer).State = EntityState.Modified; } Invoice invoice = new Invoice { Customer = customer, OrderDate = DateTime.Now }; foreach (CartLine cartLine in cart.Lines) { OrderItem orderItem = new OrderItem(cartLine.Product, cartLine.Quantity); orderItem.ProductId = cartLine.Product.ProductId; orderItem.Product = null; invoice.OrderItems.Add(orderItem); } // Save to data store dataContext.Invoices.Add(invoice); await dataContext.SaveChangesAsync(); cart.Clear(); return(View("Completed")); } else { return(View(order)); } }
public ViewResult Checkout(Cart cart, ShippingDetails shippingDetails) { if (cart.Lines.Count() == 0) { ModelState.AddModelError("", "Sorry, your cart is empty!"); } if (ModelState.IsValid) { Customer customer = new Customer { Firstname = shippingDetails.Firstname, Lastname = shippingDetails.Lastname, Address = shippingDetails.Address, Zip = shippingDetails.Zip, Email = shippingDetails.Email }; if (db.Customers.Any(c => c.Firstname == customer.Firstname && c.Lastname == customer.Lastname && c.Email == customer.Email)) { customer = db.Customers.Where(c => c.Firstname == customer.Firstname && c.Lastname == customer.Lastname && c.Email == customer.Email).First(); customer.Address = shippingDetails.Address; customer.Zip = shippingDetails.Zip; // ensure update instead of insert db.Entry(customer).State = EntityState.Modified; } // order processing logic Invoice invoice = new Invoice(DateTime.Now, customer); foreach (CartLine cartline in cart.Lines) { OrderItem orderItem = new OrderItem(cartline.Product, cartline.Quantity); orderItem.ProductId = cartline.Product.ProductId; orderItem.Product = null; invoice.OrderItems.Add(orderItem); } db.Invoices.Add(invoice); db.SaveChanges(); cart.Clear(); return(View("Completed")); } else { return(View(shippingDetails)); } }