Beispiel #1
0
        public async Task <IActionResult> Create(HangHoa hangHoa, IFormFile fHinh)
        {
            if (ModelState.IsValid)
            {
                if (fHinh != null)
                {
                    hangHoa.Hinh = MyTool.XuLyUpload(fHinh, "HangHoa");
                }

                hangHoa.TenKhongDau = MyTool.ConvertUrlFriendly(hangHoa.TenHh);
                _context.Add(hangHoa);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["MaLoai"] = new SelectList(_context.Loais, "MaLoai", "TenLoai", hangHoa.MaLoai);

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

            if (ModelState.IsValid)
            {
                try
                {
                    if (fHinh != null)
                    {
                        hangHoa.Hinh = MyTool.XuLyUpload(fHinh, "HangHoa");
                    }

                    hangHoa.TenKhongDau = MyTool.ConvertUrlFriendly(hangHoa.TenHh);

                    _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", "TenLoai", hangHoa.MaLoai);
            return(View(hangHoa));
        }