Ejemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("ID,Ten")] LoaiSanPham loaiSanPham)
        {
            if (ModelState.IsValid)
            {
                _context.Add(loaiSanPham);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(loaiSanPham));
        }
        public async Task <IActionResult> Create([Bind("ID,Ten,Hinh,Maloai")] SanPham sanPham, IFormFile ful)
        {
            if (ModelState.IsValid)
            {
                _context.Add(sanPham);
                await _context.SaveChangesAsync();

                var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/img", sanPham.ID + "." + ful.FileName.Split(".")
                                        [ful.FileName.Split(".").Length - 1]);
                using (var stream = new FileStream(path, FileMode.Create))
                {
                    await ful.CopyToAsync(stream);
                }
                sanPham.Hinh = sanPham.ID + "." + ful.FileName.Split(".")[ful.FileName.Split(".").Length - 1];
                _context.Update(sanPham);
                _ = await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            //ViewData["Maloai"] = new SelectList(_context.LoaiSanPham, "ID", "ID", sanPham.Maloai);
            return(View(sanPham));
        }