public async Task <IActionResult> Create([Bind("BikeID,Name,Description,Price,ImageTB,Preferred,Stock,CategoryID")] BikeCreateVM bike) { if (bike != null) { string fileName = null; if (bike.ImageTB != null) { string folder = Path.Combine(_hostingEnvironment.WebRootPath, "images"); fileName = Guid.NewGuid().ToString() + "_" + bike.ImageTB.FileName; string filePath = Path.Combine(folder, fileName); await bike.ImageTB.CopyToAsync(new FileStream(filePath, FileMode.Create)); } Bike newBike = new Bike { Name = bike.Name, ImageTB = fileName, Stock = bike.Stock, Description = bike.Description, Price = bike.Price, CategoryID = bike.CategoryID }; _context.Bikes.Add(newBike); await _context.SaveChangesAsync(); return(RedirectToAction("Index", new { id = newBike.BikeID })); } ViewData["CategoryID"] = new SelectList(_context.Categories, "CategoryID", "CategoryID", bike.CategoryID); return(View(bike)); }
public async Task <IActionResult> Edit(int id, [Bind("BikeID,Name,Description,Price,ImageTB,Preferred,Stock,CategoryID")] BikeCreateVM bike) { if (id != bike.BikeID) { return(NotFound()); } if (bike != null) { try { string fileName = null; if (bike.ImageTB != null) { string folder = Path.Combine(_hostingEnvironment.WebRootPath, "images"); fileName = Guid.NewGuid().ToString() + "_" + bike.ImageTB.FileName; string filePath = Path.Combine(folder, fileName); await bike.ImageTB.CopyToAsync(new FileStream(filePath, FileMode.Create)); } Bike newBike = new Bike { Name = bike.Name, ImageTB = fileName, Stock = bike.Stock, Description = bike.Description, Price = bike.Price, CategoryID = bike.CategoryID }; _context.Bikes.Update(newBike); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BikeExists(bike.BikeID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["CategoryID"] = new SelectList(_context.Categories, "CategoryID", "CategoryID", bike.CategoryID); return(View(bike)); }