Beispiel #1
0
        public async Task <IActionResult> Edit(int id)
        {
            var linhkien = await _linhKienRepository.getById(id);


            if (linhkien == null)
            {
                return(View("~/Views/Error/404.cshtml"));
            }

            LinhKienDTO dto = _mapper.Map <LinhKienDTO>(linhkien);

            //Get All Category
            var categorys = await _llkRepository.getAll();

            var categoryDTO = _mapper.Map <List <LoaiLinhKienDTO> >(categorys);

            ViewBag.Category = categoryDTO;

            //Get All Manufactuer
            var manufactuers = await _nccRepository.getAll();

            var manuDTO = _mapper.Map <List <NhaCungCapDTO> >(manufactuers);

            ViewBag.Manu = manuDTO;

            var images = dto.Hinh.Split(",");

            ViewBag.Hinh = images;

            return(View("~/Views/Admin/Product/Edit.cshtml", dto));
        }
Beispiel #2
0
        public async Task <IActionResult> EditPost(int id, [FromForm] List <IFormFile> files, [FromForm] LinhKienDTO dto, [FromForm] List <string> oldFiles)
        {
            if (oldFiles.Count == 0 && (files == null || files.Count == 0))
            {
                //Get All Category
                var categorys = await _llkRepository.getAll();

                var categoryDTO = _mapper.Map <List <LoaiLinhKienDTO> >(categorys);
                ViewBag.Category = categoryDTO;

                //Get All Manufactuer
                var manufactuers = await _nccRepository.getAll();

                var manuDTO = _mapper.Map <List <NhaCungCapDTO> >(manufactuers);
                ViewBag.Manu = manuDTO;

                var imagesE = dto.Hinh.Split(",");
                ViewBag.Hinh = imagesE;
                ModelState.AddModelError("Hinh", "Thêm ít nhất một hình");

                return(View("~/Views/Admin/Product/Edit.cshtml", dto));
            }

            if (!ModelState.IsValid)
            {
                //Get All Category
                var categorys = await _llkRepository.getAll();

                var categoryDTO = _mapper.Map <List <LoaiLinhKienDTO> >(categorys);
                ViewBag.Category = categoryDTO;

                //Get All Manufactuer
                var manufactuers = await _nccRepository.getAll();

                var manuDTO = _mapper.Map <List <NhaCungCapDTO> >(manufactuers);
                ViewBag.Manu = manuDTO;

                var imagesE = dto.Hinh.Split(",");
                ViewBag.Hinh = imagesE;

                return(View("~/Views/Admin/Product/Edit.cshtml", dto));
            }

            var lk = await _linhKienRepository.getById(id);

            var    images      = lk.Hinh.Split(",");
            string webRootPath = _webHostEnvironment.WebRootPath;
            string Hinh        = "";

            // Xóa những hình bị yêu cầu xóa
            foreach (var image in images)
            {
                var flg = false;
                foreach (var oldFile in oldFiles)
                {
                    if (oldFile == image)
                    {
                        flg   = true;
                        Hinh += oldFile + ",";
                    }
                }
                if (flg == false)
                {
                    var fullPath = webRootPath + "/images/products/" + image;
                    if (System.IO.File.Exists(fullPath))
                    {
                        System.IO.File.Delete(fullPath);
                    }
                }
            }

            //Lưu Hình vào Server
            string contentRootPath = _webHostEnvironment.ContentRootPath;
            string pathS           = "";
            Random rnd             = new Random();

            pathS = Path.Combine(contentRootPath, "images");
            pathS = Path.Combine(pathS, "products");

            foreach (var formFile in files)
            {
                if (formFile.Length > 0)
                {
                    try
                    {
                        string fileName = dto.MaLK + rnd.Next(1, 100000) + Path.GetExtension(formFile.FileName);
                        string _path    = Path.Combine(contentRootPath, "wwwroot\\images");
                        _path = Path.Combine(_path, "products");
                        _path = Path.Combine(_path, fileName);
                        using (var fileStream = new FileStream(_path, FileMode.Create))
                        {
                            await formFile.CopyToAsync(fileStream);
                        }
                        Hinh += fileName + ",";
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                }
            }
            Hinh = Hinh.Remove(Hinh.Length - 1);

            //Sửa dữ liệu
            lk.TenLK    = dto.TenLK;
            lk.MaLK     = dto.MaLK;
            lk.MoTa     = dto.MoTa;
            lk.TGBH     = dto.TGBH;
            lk.SLTonKho = dto.SLTonKho;

            var loaillk = await _llkRepository.getById(dto.LoaiId);

            var ncc = await _nccRepository.getById(dto.NCCId);

            lk.Loai        = loaillk;
            lk.NCC         = ncc;
            lk.Hinh        = Hinh;
            lk.NgayCapNhat = DateTime.Now;
            await _linhKienRepository.Update(lk);

            return(Redirect("/admin/product"));
        }