public IActionResult Create(CreateBikeViewModel createBike) { if (ModelState.IsValid) { var bike = _mapper.Map <Product>(createBike); var fileName = GetPhotoPath(createBike); bike.ProductPhoto = fileName; _productsRepo.CreateProduct(bike); return(RedirectToAction("Details", new { id = bike.ProductId })); } ViewBag.Brands = new SelectList(_brandsRepo.GetBrands(), "BrandName", "BrandName"); ViewBag.Categories = new SelectList(_categoriesRepo.GetCategories(), "CategoryName", "CategoryName"); return(View(createBike)); }
public IActionResult CreateAdvertise() { var model = new CreateBikeViewModel(); model.ProductAge = DateTime.Today; BikeAndCarViewModel combinedModel = new BikeAndCarViewModel { car = initializeCarModel(), bike = model, Currencies = new SelectList(currencyContainer.GetCurrencyNameList()) }; return(View(combinedModel)); }
private string GetPhotoPath(CreateBikeViewModel createBike) { if (createBike.ProductPhoto == null) { return("~/img/noimage.jpg"); } var uniqueFileName = "~/img/" + Guid.NewGuid().ToString() + "_" + createBike.ProductPhoto.FileName; var rootPath = _hostEnvironment.WebRootPath; string replacedFileName = uniqueFileName.Substring(1).Replace('/', '\\'); using (var stream = new FileStream(rootPath + replacedFileName, FileMode.Create)) { createBike.ProductPhoto.CopyTo(stream); } return(uniqueFileName); }