Пример #1
0
        public async Task <IActionResult> Create([Bind("ProductID,Name,Quantity,UnitPrice")] Product product)
        {
            if (ModelState.IsValid)
            {
                _context.Add(product);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(product));
        }
Пример #2
0
        //public async Task<IActionResult> Create([Bind("CartID,ShopperID,TimeSlot,Subtotal,Tax,TotalCost,CheckedOut")] Cart cart)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        _context.Add(cart);
        //        await _context.SaveChangesAsync();
        //        return RedirectToAction(nameof(Index));
        //    }
        //    ViewData["ShopperID"] = new SelectList(_context.Shopper, "ShopperID", "Email", cart.ShopperID);
        //    return View(cart);
        //}

        public async Task <IActionResult> Create([Bind("CartID,ShopperID,TimeSlot,CheckedOut")] Cart cart)
        {
            // 3) Initialize the properties of the cart



            if (ModelState.IsValid)
            {
                _context.Add(cart);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Details), "Carts", new { id = cart.CartID }));
            }
            ViewData["ShopperID"] = cart.ShopperID;
            return(View(cart));
        }
Пример #3
0
        //public async Task<IActionResult> Create([Bind("ShopperID,LastName,FirstName,Email")] Shopper shopper)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        _context.Add(shopper);
        //        await _context.SaveChangesAsync();
        //        return RedirectToAction(nameof(Index));
        //    }
        //    return View(shopper);
        //}

        /// <summary>
        /// Creates a new shopper, but only if the Email address is unique.
        /// </summary>
        /// <param name="searchString"></param>
        /// <returns></returns>
        public async Task <IActionResult> Create([Bind("ShopperID,LastName,FirstName,Email")] Shopper shopper)
        {
            var shoppers = from s in _context.Shopper
                           select s;

            shoppers = shoppers.Where(s => s.Email.Equals(shopper.Email));
            Shopper temp = shoppers.FirstOrDefault();

            if (temp == null)
            {
                if (ModelState.IsValid)
                {
                    _context.Add(shopper);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Details), "Shoppers", new { id = shopper.ShopperID }));
                }
            }

            // The user needs to have a unique Email address. Goes back to the orginal view.
            return(View(shopper));
        }
Пример #4
0
        //public async Task<IActionResult> Create([Bind("OrderID,CartID,ProductID,Quantity,Cost")] Order order)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        _context.Add(order);
        //        await _context.SaveChangesAsync();
        //        return RedirectToAction(nameof(Index));
        //    }
        //    ViewData["CartID"] = new SelectList(_context.Cart, "CartID", "CartID", order.CartID);
        //    ViewData["ProductID"] = new SelectList(_context.Set<Product>(), "ProductID", "ProductID", order.ProductID);
        //    return View(order);
        //}

        public async Task <IActionResult> Create([Bind("OrderID,CartID,ProductID,Quantity")] Order order)
        {
            Cart cart = _context.Cart.FirstOrDefault(m => m.CartID == order.CartID);

            if (cart.CheckedOut)
            {
                return(RedirectToAction(nameof(Index)));
            }

            if (ModelState.IsValid)
            {
                // 1) Include the required code to create, or modify, an order



                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Details), "Carts", new { id = order.CartID }));
            }
            ViewData["CartID"]    = order.CartID;
            ViewData["ProductID"] = new SelectList(_context.Set <Product>(), "ProductID", "ProductID", order.ProductID);
            return(View(order));
        }