Exemple #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));
        }
        //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));
        }
Exemple #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));
        }