コード例 #1
0
        public async Task <IActionResult> Create([Bind("ProductId,Name,Price,Description,Active,Slug,CategoryId")] Product product)
        {
            if (ModelState.IsValid)
            {
                _context.Add(product);

                var imgFile = Request.Form.Files["image"];

                if (imgFile != null || imgFile.Length > 0)
                {
                    Image img = new Image();
                    img.Path = imgFile.FileName;

                    var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "img", imgFile.FileName);

                    using (var stream = new FileStream(path, FileMode.Create))
                    {
                        await imgFile.CopyToAsync(stream);
                    }


                    img.Product = product;
                    _context.Images.Add(img);
                }
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CategoryId"] = new SelectList(_context.Categories, "CategoryId", "CategoryId", product.CategoryId);
            return(View(product));
        }
コード例 #2
0
        public async Task <IActionResult> Create([Bind("CategoryId,Name,Slug")] Category category)
        {
            if (ModelState.IsValid)
            {
                _context.Add(category);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
コード例 #3
0
        public async Task <IActionResult> Create([Bind("PaymentMethodId,Name")] PaymentMethod paymentMethod)
        {
            if (ModelState.IsValid)
            {
                _context.Add(paymentMethod);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(paymentMethod));
        }
コード例 #4
0
        public async Task <IActionResult> Create([Bind("OrderId,Time,CustomerName,Note,ShipAdress,ContactNumber,PaymentId")] Order order)
        {
            if (ModelState.IsValid)
            {
                _context.Add(order);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["PaymentId"] = new SelectList(_context.Payments, "PaymentId", "PaymentId", order.PaymentId);
            return(View(order));
        }
コード例 #5
0
        public async Task <IActionResult> Create([Bind("ImageId,Path,ProductId")] Image image)
        {
            if (ModelState.IsValid)
            {
                _context.Add(image);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ProductId"] = new SelectList(_context.Products, "ProductId", "ProductId", image.ProductId);
            return(View(image));
        }