public async Task <ActionResult> Post([FromForm] CarViewModel carVM) { if (carVM.Image != null) { var a = _hostingEnv.WebRootPath; var fileName = Path.GetFileName(carVM.Image.FileName); var filePath = Path.Combine(_hostingEnv.WebRootPath, "images\\Cars", fileName); using (var fileSteam = new FileStream(filePath, FileMode.Create)) { await carVM.Image.CopyToAsync(fileSteam); } Car car = new Car(); car.CarName = carVM.CarName; car.ImagePath = filePath; //save the filePath to database ImagePath field. _context.Add(car); await _context.SaveChangesAsync(); return(Ok()); } else { return(BadRequest()); } }
public async Task <bool> SaveAsync() { return(await _context.SaveChangesAsync() >= 0); }