public async Task CreateAsync(CreateProductViewModelImages model, List <string> imagePaths, string userId) { var images = new List <Image>(); foreach (var imagePath in imagePaths) { var image = new Image() { Url = imagePath, ProductId = model.Id, }; images.Add(image); } var product = new Product { Name = model.Name, Description = model.Description, MoreInfo = model.MoreInfo, Price = model.Price, Quantity = model.Quantity, CategoryId = model.CategoryId, Images = images, UserId = userId, }; await this.productRepository.AddAsync(product); await this.productRepository.SaveChangesAsync(); }
public IActionResult Add() { var viewModel = new CreateProductViewModelImages { CategoriesItems = this._categoriesService.GetAllAsKeyValuePairs(), }; return(this.View(viewModel)); }
public async Task <IActionResult> Add(CreateProductViewModelImages input) { if (!this.ModelState.IsValid) { input.CategoriesItems = this._categoriesService.GetAllAsKeyValuePairs(); return(this.View(input)); } var imageResult = await CloudinaryExtentsion.UploadAsync(this.cloudinary, input.Images); var user = await this._userManager.GetUserAsync(this.User); await this._productService.CreateAsync(input, imageResult, user.Id); return(this.RedirectToAction("Add")); }