コード例 #1
0
        public async Task <IActionResult> Edit(SmartFuture smartFuture)
        {
            if (!ModelState.IsValid)
            {
                return(View(smartFuture));
            }
            var smartfutureDb = await _context.SmartFutures.FindAsync(smartFuture.Id);

            if (smartFuture.Photo != null)
            {
                try
                {
                    var newPhoto = await smartFuture.Photo.SaveFileAsync(_env.WebRootPath, "images");

                    IFormFileExstensions.Delete(_env.WebRootPath, "images", smartfutureDb.Image);

                    smartfutureDb.Image = newPhoto;
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("", "Unexpected error happened while saving image. Please, try again.");
                    return(View(smartFuture));
                }
            }

            smartfutureDb.Title    = smartFuture.Title;
            smartfutureDb.Subtitle = smartFuture.Subtitle;
            smartfutureDb.Button   = smartFuture.Button;


            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> DeleteProduct(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Product product = await _context.Products.FindAsync(id);

            if (product == null)
            {
                return(NotFound());
            }


            foreach (var item in product.Images)
            {
                IFormFileExstensions.Delete(_env.WebRootPath, "images", item.ProductImage);
            }
            product.Status = false;
            await _context.SaveChangesAsync();


            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> Edit(GloballyConnected globallyConnected)
        {
            if (!ModelState.IsValid)
            {
                return(View(globallyConnected));
            }
            var globallyConnectedDb = await _context.GloballyConnecteds.FindAsync(globallyConnected.Id);

            if (globallyConnected.Photo != null)
            {
                try
                {
                    var newPhoto = await globallyConnected.Photo.SaveFileAsync(_env.WebRootPath, "images");

                    IFormFileExstensions.Delete(_env.WebRootPath, "images", globallyConnectedDb.Image);

                    globallyConnectedDb.Image = newPhoto;
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("", "Unexpected error happened while saving image. Please, try again.");
                    return(View(globallyConnected));
                }
            }

            globallyConnectedDb.Title     = globallyConnected.Title;
            globallyConnectedDb.Subtitle  = globallyConnected.Subtitle;
            globallyConnectedDb.Paragraph = globallyConnected.Paragraph;
            globallyConnectedDb.Button    = globallyConnected.Button;

            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> Edit(HomePageVM homePageVM)
        {
            Product product = await _context.Products.FindAsync(homePageVM.Product.Id);

            var        image       = _context.Images.Where(i => i.ProductId == product.Id).ToList();
            HomePageVM homePageVMs = new HomePageVM()
            {
                Product = product,
                Images  = image
            };

            if (!ModelState.IsValid)
            {
                return(View(homePageVMs));
            }
            var productDb = await _context.Products.FindAsync(homePageVM.Product.Id);

            if (homePageVM.ProductPhotos != null)
            {
                try
                {
                    var ImgList = _context.Images.Where(v => v.ProductId == productDb.Id).ToList();
                    for (int i = 0; i < homePageVM.ProductPhotos.Count; i++)
                    {
                        IFormFileExstensions.Delete(_env.WebRootPath, "images", ImgList[i].ProductImage);

                        var newPhoto = await homePageVM.ProductPhotos[i].SaveFileAsync(_env.WebRootPath, "images");


                        ImgList[i].ProductImage = newPhoto;
                        _context.SaveChanges();
                    }
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("", "Unexpected error happened while saving image. Please, try again.");
                    return(View(homePageVMs));
                }
            }

            productDb.Name            = homePageVM.Product.Name;
            productDb.Price           = homePageVM.Product.Price;
            productDb.HasDiscount     = homePageVM.Product.HasDiscount;
            productDb.DiscountedPrice = homePageVM.Product.DiscountedPrice;
            productDb.Color           = homePageVM.Product.Color;
            productDb.Availability    = homePageVM.Product.Availability;
            productDb.Status          = true;
            productDb.InStock         = homePageVM.Product.InStock;
            productDb.BrandId         = homePageVM.Product.BrandId;

            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> Edit(BrandNewApp brandNewApp)
        {
            if (!ModelState.IsValid)
            {
                return(View(brandNewApp));
            }
            var brandNewAppDb = await _context.BrandNewApps.FindAsync(brandNewApp.Id);

            if (brandNewApp.Photo != null)
            {
                try
                {
                    var newPhoto = await brandNewApp.Photo.SaveFileAsync(_env.WebRootPath, "images");

                    IFormFileExstensions.Delete(_env.WebRootPath, "images", brandNewAppDb.Image);

                    brandNewAppDb.Image = newPhoto;
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("", "Unexpected error happened while saving image. Please, try again.");
                    return(View(brandNewApp));
                }
            }

            if (brandNewApp.IconPhoto != null)
            {
                try
                {
                    var icoPhoto = await brandNewApp.IconPhoto.SaveFileAsync(_env.WebRootPath, "images");

                    IFormFileExstensions.Delete(_env.WebRootPath, "images", brandNewAppDb.IconImage);

                    brandNewAppDb.IconImage = icoPhoto;
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("", "Unexpected error happened while saving image. Please, try again.");
                    return(View(brandNewApp));
                }
            }

            brandNewAppDb.SubtitleTop    = brandNewApp.SubtitleTop;
            brandNewAppDb.Title          = brandNewApp.Title;
            brandNewAppDb.SubtitleBottom = brandNewApp.SubtitleBottom;
            brandNewAppDb.Paragraph      = brandNewApp.Paragraph;
            brandNewAppDb.Button         = brandNewApp.Button;



            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }