コード例 #1
0
        public async Task <IActionResult> Edit(int id, [Bind("BagpackID,Name,Price,ProductID")] Bagpack bagpack)
        {
            if (id != bagpack.BagpackID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(bagpack);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!BagpackExists(bagpack.BagpackID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(bagpack));
        }
コード例 #2
0
        public async Task <IActionResult> Create([Bind("BagpackID,Name,Price,ProductID")] Bagpack bagpack)
        {
            if (ModelState.IsValid)
            {
                _context.Add(bagpack);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(bagpack));
        }
コード例 #3
0
        public async Task <IActionResult> AddToCart([Bind("BagpackID,Name,Price,ProductID")] Bagpack bagpack,
                                                    Cart cart, int id)
        {
            if (ModelState.IsValid)
            {
                var    client_obj  = _context.Clients.FirstOrDefault(o => o.Name == "andrzej");
                int    client_id   = client_obj.ClientID;
                var    product_obj = _context.Products.FirstOrDefault(o => o.Name == "Bagpack");
                string name        = product_obj.Name;
                int    product_id  = product_obj.ProductID;
                var    bagpack_obj = _context.Bagpacks.FirstOrDefault(o => o.BagpackID == id);
                cart.Quantity     = 1;
                cart.Product_Name = name;
                cart.Model_Name   = bagpack_obj.Name;
                cart.Price        = bagpack_obj.Price;
                cart.ProductID    = product_id;
                cart.ClientID     = client_id;
                _context.Add(cart);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(cart));
        }
コード例 #4
0
        public static void Initialize(Sport_shopDbContext context)
        {
            context.Database.EnsureCreated();


            //Look for any Bagpacks
            if (context.Bagpacks.Any())
            {
                return;
            }

            var products = new Product[]
            {
                new Product {
                    Name = "Bagpack"
                },
                new Product {
                    Name = "Bicycle"
                },
                new Product {
                    Name = "Football"
                },
                new Product {
                    Name = "Shoe"
                },
            };

            foreach (Product product in products)
            {
                context.Products.Add(product);
            }
            context.SaveChanges();


            var bagpacks = new Bagpack[]
            {
                new Bagpack {
                    Name = "Model 1", Price = 30, ProductID = 1
                },
                new Bagpack {
                    Name = "Model 2", Price = 40, ProductID = 1
                },
                new Bagpack {
                    Name = "Model 3", Price = 50, ProductID = 1
                },
                new Bagpack {
                    Name = "Model 4", Price = 60, ProductID = 1
                },
            };

            foreach (Bagpack bagpack in bagpacks)
            {
                context.Bagpacks.Add(bagpack);
            }
            context.SaveChanges();

            var bicycles = new Bicycle[]
            {
                new Bicycle {
                    Name = "Model 1", Price = 530, ProductID = 2
                },
                new Bicycle {
                    Name = "Model 2", Price = 540, ProductID = 2
                },
                new Bicycle {
                    Name = "Model 3", Price = 550, ProductID = 2
                },
                new Bicycle {
                    Name = "Model 4", Price = 560, ProductID = 2
                },
            };

            foreach (Bicycle bicycle in bicycles)
            {
                context.Bicycles.Add(bicycle);
            }
            context.SaveChanges();

            var footballs = new Football[]
            {
                new Football {
                    Name = "Model 1", Price = 70, ProductID = 3
                },
                new Football {
                    Name = "Model 2", Price = 80, ProductID = 3
                },
                new Football {
                    Name = "Model 3", Price = 90, ProductID = 3
                },
                new Football {
                    Name = "Model 4", Price = 100, ProductID = 3
                },
            };

            foreach (Football football in footballs)
            {
                context.Footballs.Add(football);
            }
            context.SaveChanges();

            var shoes = new Shoe[]
            {
                new Shoe {
                    Name = "Model 1", Price = 70, ProductID = 4
                },
                new Shoe {
                    Name = "Model 2", Price = 80, ProductID = 4
                },
                new Shoe {
                    Name = "Model 3", Price = 90, ProductID = 4
                },
                new Shoe {
                    Name = "Model 4", Price = 100, ProductID = 4
                },
            };

            foreach (Shoe shoe in shoes)
            {
                context.Shoes.Add(shoe);
            }
            context.SaveChanges();
        }