コード例 #1
0
        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());
        }
コード例 #2
0
        public async Task <IActionResult> Create([Bind("Author,Publisher,Published,ISBN,ProductId,Title,Price,ImageUrl,Category,CreatedDate")] Book book)
        {
            if (ModelState.IsValid)
            {
                _context.Add(book);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(book));
        }
コード例 #3
0
        public async Task <IActionResult> Create([Bind("Artist,Label,Released,ProductId,Title,Price,ImageUrl,Category")] MusicCD musicCD)
        {
            if (ModelState.IsValid)
            {
                _context.Add(musicCD);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(musicCD));
        }
コード例 #4
0
        public async Task <IActionResult> Create([Bind("Director,Released,ProductId,Title,Price,ImageUrl,Category,CreatedDate")] Movie movie)
        {
            if (ModelState.IsValid)
            {
                _context.Add(movie);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(movie));
        }
コード例 #5
0
        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));
            }
        }
コード例 #6
0
        public async Task <IActionResult> Create([Bind("Artist,Label,Released,Title,Price,ImageUrl,Category")] MusicCD musicCD)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _context.Add(musicCD);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(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(musicCD));
        }