Beispiel #1
0
        public async Task <IActionResult> Create([Bind("checkoutId,checkoutLicensePlate,checkoutTime")] CarCheckOut carCheckOut)
        {
            if (ModelState.IsValid)
            {
                _context.Add(carCheckOut);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(carCheckOut));
        }
        public async Task <IActionResult> Create(Product product)
        {
            if (ModelState.IsValid)
            {
                product.createdDate = DateTime.Now;
                _context.Add(product);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(product));
        }
Beispiel #3
0
        public async Task<IActionResult> addCart(List<Item> items){
           
        
            //check to seee if the items size >0
            if(items.Count >0){
                
                //create new cart object
               Cart cart = new Cart{
                   createdDate = DateTime.Now
               };

               //add cart into carts context
               _context.carts.Add(cart);

               //save change to the new cart
               //note thate when the new cart object is saved
               //the system will automatically assign the primary key(cartId) data for each cart data row
               await _context.SaveChangesAsync(); //save cart first

               //after the operation above, cart.cartId is accessible

               //create a list of cart items
               List<CartItem> list1 = new List<CartItem>();

               //loop throught each item in the items list
               foreach(Item item in items){
                    //create a new object with the structure based on Model>CartItem.cs 
                    var row = new CartItem {
                        productId = item.productId,
                        productQty = item.productQty,
                        productPrice = item.productPrice,
                        cartId     = cart.cartId  //cart Id go here
                    };
                    list1.Add(row);
               }//end loop
               
               //assign caritem list 
               cart.cartItems = list1;
               await _context.SaveChangesAsync(); //save cart first
                //step33: return Action to be call by client
                return Json(new
                {
                    newUrl = Url.Action("Index")
                }
             );
            }
            else{
                return Content("error data upload fail");
            }
        
        }