Example #1
0
        public async Task <IActionResult> Edit(int id)
        {
            ViewBag.Title = "Sửa Chủng Loại";
            LoaiLinhKien loaiLinhKien = await _loaiLinhKienRepo.getById(id);

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

            LoaiLinhKienDTO dto = _mapper.Map <LoaiLinhKienDTO>(loaiLinhKien);

            return(View("~/Views/Admin/Category/Edit.cshtml", dto));
        }
Example #2
0
        public async Task <IActionResult> Category(int id)
        {
            var products = await _linhkienRepo.Filter(searchCategory : id);

            if (products.Count() == 0)
            {
                return(View("~/Views/Error/404.cshtml"));
            }
            var result = new List <LinhKienDTO>();

            foreach (var item in products)
            {
                item.DonGias = item.DonGias.Where(i => i.ApDung).ToList();
                var dto = mapper.Map <LinhKienDTO>(item);
                result.Add(dto);
            }
            var list = new HtmlString(JsonConvert.SerializeObject(result, Formatting.None,
                                                                  new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            }));
            var tenloai = (await _loailinhkienRepo.getById(id)).Ten;

            ViewData["list"]    = list;
            ViewData["TenLoai"] = tenloai;
            return(View());
        }
Example #3
0
        public async Task <IActionResult> AddPost([FromForm] ProductAddViewModel 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;

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

            //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");
            string Hinh = "";

            foreach (var formFile in dto.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
                    {
                        //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;
                        ModelState.AddModelError("Hinh", "Upload hình không thành công");
                        return(View("~/Views/Admin/Product/Add.cshtml", dto));
                    }
                }
            }
            Hinh = Hinh.Remove(Hinh.Length - 1);

            //Create Entity
            LinhKien lk = new LinhKien();

            lk.TenLK    = dto.TenLK;
            lk.MaLK     = dto.MaLK;
            lk.MoTa     = dto.MoTa;
            lk.TGBH     = dto.TGBH;
            lk.SLTonKho = dto.SLTonKho;

            DonGia donGia = new DonGia();

            donGia.DonGiaBan = (decimal)dto.Gia;
            donGia.GiamGia   = dto.GiamGia;
            donGia.ApDung    = true;
            donGia.Ngay      = DateTime.Now.Date;

            List <DonGia> donGias = new List <DonGia>();

            donGias.Add(donGia);
            lk.DonGias = donGias;

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

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

            lk.Loai = loaillk;
            lk.NCC  = ncc;
            lk.Hinh = Hinh;

            await _linhKienRepository.Add(lk);

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