public async Task <IActionResult> Edit(int id, [Bind("MaHh,TenHh,DonGia,SoLuong,Hinh,MoTa,MaLoai")] HangHoa hangHoa, IFormFile fHinh)
        {
            if (id != hangHoa.MaHh)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (fHinh != null)
                    {
                        hangHoa.Hinh = MyTool.UploadHinh(fHinh, "HangHoa");
                    }
                    _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.Loais, "MaLoai", "MaLoai", hangHoa.MaLoai);
            return(View(hangHoa));
        }
        public async Task <IActionResult> Create([Bind("MaHh,TenHh,DonGia,SoLuong,Hinh,MoTa,MaLoai")] HangHoa hangHoa, IFormFile fHinh)
        {
            if (ModelState.IsValid)
            {
                hangHoa.Hinh = MyTool.UploadHinh(fHinh, "HangHoa");
                _context.Add(hangHoa);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["MaLoai"] = new SelectList(_context.Loais, "MaLoai", "MaLoai", hangHoa.MaLoai);
            return(View(hangHoa));
        }
        public IActionResult UploadImage([FromForm] IFormFile file)
        {
            var info = HttpContext.Session.GetObject <Employee>("Employee");

            if (info == null)
            {
                return(BadRequest(new { message = "login" }));
            }

            string fileName = file.FileName;
            bool   check    = fileName[0] == 'A'; // check Add or Edit

            string id = "";

            for (int i = 2; i < fileName.Length; ++i)
            {
                if (fileName[i] != '_')
                {
                    id += fileName[i];
                }
                else
                {
                    break;
                }
            }



            if (file != null)
            {
                string folder = "";
                if (!check)
                {
                    var emp = _context.Employee.SingleOrDefault(e => e.EmployeeId == int.Parse(id));
                    folder = emp.EmployeeId + "_Employee_" + emp.CreatedDate?.ToString("yyyyMMddHHmmss");
                }
                else
                {
                    folder = "_Employee_" + id;
                }
                var pathString = "wwwroot/Image/" + folder;
                Directory.CreateDirectory(pathString);
                var name = MyTool.UploadHinh(file, folder);
                return(Ok(new { name = name, folder = folder }));
            }
            return(BadRequest());
        }
Beispiel #4
0
        public async Task <IActionResult> Create(HangHoa hangHoa, IFormFile fHinh)
        {
            if (ModelState.IsValid)
            {
                hangHoa.Hinh = MyTool.UploadHinh(fHinh, "HangHoa");
                _context.Add(hangHoa);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            //ViewData["MaLoai"] = new SelectList(_context.Loais, "MaLoai", "TenLoai", hangHoa.MaLoai);
            ViewBag.Loai = new MySelectList <Loai>
            {
                Data     = _context.Loais.ToList(),
                Selected = hangHoa.MaLoai
            };
            ViewBag.ThuongHieu = new SelectList(_context.ThuongHieus, "MaTh", "TenThuongHieu", hangHoa.MaTh);
            return(View(hangHoa));
        }
Beispiel #5
0
        public async Task <IActionResult> Edit(int id, HangHoa hangHoa, IFormFile fHinh)
        {
            if (id != hangHoa.MaHh)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (fHinh != null)
                    {
                        hangHoa.Hinh = MyTool.UploadHinh(fHinh, "HangHoa");
                    }
                    _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.Loais, "MaLoai", "MaLoai", hangHoa.MaLoai);
            ViewBag.Loai = new MySelectList <Loai>
            {
                Data     = _context.Loais.ToList(),
                Selected = hangHoa.MaLoai
            };
            ViewBag.ThuongHieu = new SelectList(_context.ThuongHieus, "MaTh", "TenThuongHieu", hangHoa.MaTh);
            return(View(hangHoa));
        }