Beispiel #1
0
        public IActionResult Create(BannerModel model, IFormFile image)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (model.ID == 0)
                    {
                        #region For Create

                        var banner = _mapper.Map <Banner>(model);
                        if (image != null)
                        {
                            string urlImage = MyTool.UploadImage(image, "wwwroot", "Image", "Sliders");
                            if (!string.IsNullOrEmpty(urlImage))
                            {
                                banner.ImagePath = urlImage;
                            }
                        }

                        db.Banners.Add(banner);
                        db.SaveChanges();

                        #endregion For Create
                        TempData["StatusMessage"] = "Successfully created";
                    }
                    else
                    {
                        #region for edit

                        var bannerEdit = _mapper.Map <Banner>(model);

                        if (image != null)
                        {
                            string urlImage = MyTool.UploadImage(image, "wwwroot", "Image", "Sliders");
                            if (!string.IsNullOrEmpty(urlImage))
                            {
                                bannerEdit.ImagePath = urlImage;
                            }
                        }

                        db.Update(bannerEdit);
                        db.SaveChanges();

                        #endregion for edit
                        TempData["StatusMessage"] = "Successfully updated";
                    }

                    return(RedirectToAction("Index"));
                }
            }
            catch (Exception ex)
            {
                TempData["StatusMessage"] = ex.Message;

                return(View(model));
            }

            return(View(model));
        }
Beispiel #2
0
        public IActionResult Edit(HangHoa hangHoa, IFormFile HinhEdit)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.Loai = new SelectList(_context.Loais, "MaLoai", "TenLoai", hangHoa.MaLoai);
                return(View());
            }

            if (HinhEdit != null)
            {
                var hinhFileName = MyTool.UploadImage(HinhEdit, "wwwroot", "Hinh", "HangHoa");
                if (!string.IsNullOrEmpty(hinhFileName))
                {
                    hangHoa.Hinh = hinhFileName;
                }
            }

            try
            {
                _context.Update(hangHoa);
                _context.SaveChanges();
                return(RedirectToAction("Index"));
            }
            catch {
                ViewBag.Loai = new SelectList(_context.Loais, "MaLoai", "TenLoai", hangHoa.MaLoai);
                return(View());
            }
        }
Beispiel #3
0
        public IActionResult Create(HangHoaVM model, IFormFile Hinh)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.Loai = new SelectList(_context.Loais, "MaLoai", "TenLoai");
                return(View());
            }

            try
            {
                var hangHoa = _mapper.Map <HangHoa>(model);

                hangHoa.Hinh = MyTool.UploadImage(Hinh, "wwwroot", "Hinh", "HangHoa");

                //Xử lý cho SKU
                var hangHoaCuoiCung = _context.HangHoas.OrderByDescending(p => p.SKU).FirstOrDefault();
                var stt             = 1;
                if (hangHoaCuoiCung != null)
                {
                    stt = int.Parse(hangHoaCuoiCung.SKU.Substring(2)) + 1;
                }
                hangHoa.SKU = "HH" + stt.ToString("00000");

                _context.Add(hangHoa);
                _context.SaveChanges();

                return(RedirectToAction("Index"));
            }
            catch
            {
                ViewBag.Loai = new SelectList(_context.Loais, "MaLoai", "TenLoai");
                return(View());
            }
        }
        public async Task <IActionResult> Create(HangHoa hangHoa, IFormFile fHinh)
        {
            if (ModelState.IsValid)
            {
                hangHoa.Hinh = MyTool.UploadImage(fHinh);

                _context.Add(hangHoa);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["MaLoai"] = new SelectList(_context.Loai, "MaLoai", "TenLoai", hangHoa.MaLoai);
            ViewData["MaNcc"]  = new SelectList(_context.NhaCungCap, "MaNcc", "TenCongTy", hangHoa.MaNcc);
            return(View(hangHoa));
        }
        public async Task <IActionResult> Edit(int id, [Bind("MaHh,TenHh,MaLoai,MoTaDonVi,DonGia,Hinh,NgaySx,GiamGia,SoLanXem,MoTa,MaNcc")] HangHoa hangHoa, IFormFile fHinh)
        {
            if (id != hangHoa.MaHh)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                if (fHinh != null)
                {
                    hangHoa.Hinh = MyTool.UploadImage(fHinh);
                }

                try
                {
                    _context.Update(hangHoa);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!HangHoaExists(hangHoa.MaHh))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["MaLoai"] = new SelectList(_context.Loai, "MaLoai", "TenLoai", hangHoa.MaLoai);
            ViewData["MaNcc"]  = new SelectList(_context.NhaCungCap, "MaNcc", "MaNcc", hangHoa.MaNcc);
            return(View(hangHoa));
        }
Beispiel #6
0
        public IActionResult Create(CategoryModel model, IFormFile image)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (model.ID == 0)
                    {
                        #region For Create

                        var category = _mapper.Map <Category>(model);
                        if (image != null)
                        {
                            string urlImage = MyTool.UploadImage(image, "wwwroot", "Image", "Categories");
                            if (!string.IsNullOrEmpty(urlImage))
                            {
                                category.Image = urlImage;
                            }
                        }

                        db.Categories.Add(category);
                        db.SaveChanges();

                        #endregion For Create
                        TempData["StatusMessage"] = "Successfully created";
                    }
                    else
                    {
                        #region for edit

                        //var categoryEdit = db.Categories.SingleOrDefault(x=> x.ID == model.ID);
                        //Require make a new instane of Category ty
                        var categoryEdit = _mapper.Map <Category>(model);
                        //categoryEdit = _mapper.Map(model, categoryEdit);
                        if (image != null)
                        {
                            string urlImage = MyTool.UploadImage(image, "wwwroot", "Image", "Categories");
                            if (!string.IsNullOrEmpty(urlImage))
                            {
                                categoryEdit.Image = urlImage;
                            }
                        }

                        db.Update(categoryEdit);
                        db.SaveChanges();

                        #endregion for edit
                        TempData["StatusMessage"] = "Successfully updated";
                    }

                    return(RedirectToAction("Index"));
                }
            }
            catch (Exception ex)
            {
                TempData["StatusMessage"] = ex.Message;

                #region Load category for dropdownlist

                List <Category> cateList = db.Categories.ToList();
                model.LstParentCategory = new List <SelectListItem>();

                var tempItem = new SelectListItem();

                tempItem.Text  = "----Assign parent category role----";
                tempItem.Value = "0";
                model.LstParentCategory.Add(tempItem);
                BindTree(cateList, null, model.LstParentCategory);

                #endregion Load category for dropdownlist

                return(View(model));
            }

            #region Load category for dropdownlist

            List <Category> categoryList = db.Categories.ToList();
            model.LstParentCategory = new List <SelectListItem>();

            var temp = new SelectListItem();

            temp.Text  = "----Assign parent category role----";
            temp.Value = "0";
            model.LstParentCategory.Add(temp);
            BindTree(categoryList, null, model.LstParentCategory);

            #endregion Load category for dropdownlist

            return(View(model));
        }