public IActionResult ChinhSua(
            SanPhamViewModel model, IFormFile AnhDaiDien,
            IFormCollection fc,
            IFormFile productImages1,
            IFormFile productImages2,
            IFormFile productImages3)
        {
            model.MaSp = fc["MaSp"];
            SanPhamViewModel a = _spService.GetById(fc["MaSp"]);

            if (AnhDaiDien == null)
            {
                model.AnhDaiDien = a.AnhDaiDien;
            }
            else
            {
                model.AnhDaiDien = FileHelper.UploadedFile(AnhDaiDien, "ProductAvatar");
            }
            if (ModelState.IsValid)
            {
                model.Ishot  = Convert.ToBoolean(fc["isHot"].ToString().Split(',')[0]);
                model.Isnew  = Convert.ToBoolean(fc["isNew"].ToString().Split(',')[0]);
                model.Status = fc["status"].ToString().Contains("on") ? 1 : 0;
                _spService.Update(model, fc["MaSp"]);
                AnhSanPhamViewModel pic = _aspService.GetByIdSp(model.MaSp);
                if (productImages1 != null)
                {
                    pic.Anh1 = FileHelper.UploadedFile(productImages1, "ProductImages");
                }
                if (productImages2 != null)
                {
                    pic.Anh2 = FileHelper.UploadedFile(productImages2, "ProductImages");
                }
                if (productImages3 != null)
                {
                    pic.Anh3 = FileHelper.UploadedFile(productImages3, "ProductImages");
                }

                _aspService.UpdateAnhSP(pic);

                foreach (var tskt in _tsktService.GetThongSoKiThuat(model.MaSp))
                {
                    tskt.GiaTri = fc[tskt.ThongSo];
                    _tsktService.UpdateTSKT(tskt);
                }
                _spService.SaveChange();
                return(RedirectToAction("QuanLy", "Admin", new { id = model.LoaiSp }).WithSuccess("Thành công", "Sản phẩm đã được sửa. ID: " + model.MaSp));
            }
            else
            {
                return(View(model));
            }
        }
        public IActionResult Them(SanPhamViewModel model,
                                  IFormFile AnhDaiDien,
                                  IFormCollection fc,
                                  IFormFile productImages1,
                                  IFormFile productImages2,
                                  IFormFile productImages3
                                  )
        {
            if (ModelState.IsValid)
            {
                model.AnhDaiDien  = FileHelper.UploadedFile(AnhDaiDien, "ProductAvatar");
                model.SoLuotXemSp = 0;
                model.Ishot       = Convert.ToBoolean(fc["isHot"].ToString().Split(',')[0]);
                model.Isnew       = Convert.ToBoolean(fc["isNew"].ToString().Split(',')[0]);

                _spService.Add(model);
                AnhSanPhamViewModel pic = new AnhSanPhamViewModel()
                {
                    MaSp = model.MaSp,

                    Anh1                                                        = productImages1 != null?FileHelper.UploadedFile(productImages1, "ProductImages") : null,
                                                     Anh2                       = productImages2 != null?FileHelper.UploadedFile(productImages2, "ProductImages") : null,
                                                                           Anh3 = productImages3 != null?FileHelper.UploadedFile(productImages3, "ProductImages") : null,
                };
                _aspService.Add(pic);
                ThongSoKiThuatViewModel tskt;
                List <ThongSoViewModel> listTS = _tsService.GetThongSo(model.LoaiSp).ToList();
                for (int i = 0; i < listTS.Count(); i++)
                {
                    tskt = new ThongSoKiThuatViewModel()
                    {
                        MaSp    = model.MaSp,
                        ThongSo = listTS[i].MaThongSo,
                        GiaTri  = fc[listTS[i].MaThongSo],
                    };

                    //context.ThongSoKiThuat.AddAsync(tskt);
                    //context.SaveChanges();
                    _tsktService.AddTSKT(tskt);
                    tskt = null;
                }
                _spService.SaveChange();
                return(RedirectToAction("QuanLy", "Admin", new { id = model.LoaiSp }).WithSuccess("Thành công", "Sản phẩm đã được thêm. ID:" + model.MaSp));
            }
            else
            {
                ModelState.AddModelError("", "aaa");
                ViewData["MaLoai"] = model.LoaiSp;
                return(View(model));
            }
        }
Beispiel #3
0
 public void UpdateAnhSP(AnhSanPhamViewModel asp)
 {
     _anhSanPhamRepository.UpdateAnhSP(_mapper.Map <AnhSanPham>(asp));
 }
Beispiel #4
0
 public void Add(AnhSanPhamViewModel asp)
 {
     _anhSanPhamRepository.Add(_mapper.Map <AnhSanPham>(asp));
 }