Exemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("ProId,Title,Author,Description,ISBN,PriceList,Price,Price50,Price100,CovId,CatId")] Product product)
        {
            var a = product;

            if (ModelState.IsValid)
            {
                var    img         = HttpContext.Request.Form.Files[0];
                string webRootPath = _hostEnvironment.WebRootPath;
                if (img != null)
                {
                    string fileName = Guid.NewGuid().ToString() + Path.GetExtension(img.FileName);
                    var    route    = Path.Combine(webRootPath, "Images\\Products");
                    using (var filesStreams = new FileStream(Path.Combine(route, fileName), FileMode.Create))
                        img.CopyTo(filesStreams);
                    product.Image = fileName;
                }
                await _context.Products.Add(product);

                await _context.Save();

                return(RedirectToAction(nameof(Index)));
            }
            ProductVM productVM = new ProductVM(product);

            productVM.FillCategory(await _context.Categories.GetAll());
            productVM.FillCoverType(await _context.CoverTypes.GetAll());
            return(View("Upsert", productVM));
        }
Exemplo n.º 2
0
        // GET: Admin/Categories/Create
        public async Task <IActionResult> Create()
        {
            ViewData["title"] = "Create New Product";
            ProductVM pro = new ProductVM(new Product());

            pro.FillCategory(await _context.Categories.GetAll());
            pro.FillCoverType(await _context.CoverTypes.GetAll());
            return(View("Upsert", pro));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Edit(int id, [Bind("ProId,Title,Author,Description,ISBN,Image,PriceList,Price,Price50,Price100,CovId,CatId")] Product product)
        {
            if (id != product.ProId)
            {
                return(NotFound());
            }

            var    Image       = HttpContext.Request.Form.Files;
            string webRootPath = _hostEnvironment.WebRootPath;

            if (ModelState.IsValid)
            {
                try
                {
                    if (Image.Count > 0)
                    {
                        var imagePath = Path.Combine(webRootPath, product.Image.TrimStart('\\'));
                        if (System.IO.File.Exists(imagePath))
                        {
                            System.IO.File.Delete(imagePath);
                        }

                        string fileName = Guid.NewGuid().ToString() + Path.GetExtension(Image[0].FileName);
                        var    route    = Path.Combine(webRootPath, "Images\\Products");
                        using (var filesStreams = new FileStream(Path.Combine(route, fileName), FileMode.Create))
                            Image[0].CopyTo(filesStreams);
                        product.Image = fileName;
                    }
                    _context.Products.Update(product);
                    await _context.Save();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProductExists(product.ProId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ProductVM productVM = new ProductVM(product);

            productVM.FillCategory(await _context.Categories.GetAll());
            productVM.FillCoverType(await _context.CoverTypes.GetAll());
            return(View("Upsert", productVM));
        }
Exemplo n.º 4
0
        // GET: Admin/Products/Edit/
        public async Task <IActionResult> Edit(int id = 0)
        {
            if (id == 0)
            {
                return(NotFound());
            }

            var product = await _context.Products.GetByID(id);

            if (product == null)
            {
                return(NotFound());
            }
            ProductVM productVM = new ProductVM(product);

            productVM.FillCategory(await _context.Categories.GetAll());
            productVM.FillCoverType(await _context.CoverTypes.GetAll());
            ViewData["title"] = $"Edit Product";
            return(View("Upsert", productVM));
        }