public async Task <IActionResult> Create([Bind(include: "")] ProductDashoardViewModel createdashVM) { try { if (ModelState["Name"].ValidationState == ModelValidationState.Invalid || ModelState["Title"].ValidationState == ModelValidationState.Invalid || ModelState["Description"].ValidationState == ModelValidationState.Invalid || ModelState["Count"].ValidationState == ModelValidationState.Invalid || ModelState["Price"].ValidationState == ModelValidationState.Invalid || ModelState["DiscountProduct"].ValidationState == ModelValidationState.Invalid || ModelState["ProductDedline"].ValidationState == ModelValidationState.Invalid || ModelState["PhotoList"].ValidationState == ModelValidationState.Invalid) { return(RedirectToAction(nameof(Index))); } if (createdashVM.PhotoList.Count != 0) { if (createdashVM.DiscountProduct == 0) { createdashVM.ProductDedline = null; } ; #region product = new Product() { Name = createdashVM.Name, Title = createdashVM.Title, Description = createdashVM.Description, Price = createdashVM.Price, Count = createdashVM.Count, DiscountProduct = createdashVM.DiscountProduct, ProductDedline = createdashVM.ProductDedline, CreatedDate = DateTime.Now, MostView = createdashVM.MostView, SellerCount = 0, Active = null }; #endregion if (!_remindb.CategoryMarkas.Any(cm => cm.CategoryId == createdashVM.CategoryId && cm.MarkaId == createdashVM.MarkaId)) { CategoryMarka categoryMarka = new CategoryMarka() { CategoryId = createdashVM.CategoryId, MarkaId = createdashVM.MarkaId, }; await _remindb.CategoryMarkas.AddAsync(categoryMarka); await _remindb.SaveChangesAsync(); product.CategoryMarkaId = categoryMarka.Id; } else { product.CategoryMarkaId = _remindb.CategoryMarkas.FirstOrDefault(cm => cm.CategoryId == createdashVM.CategoryId && cm.MarkaId == createdashVM.MarkaId).Id; }; _remindb.Products.Add(product); _remindb.SaveChanges(); foreach (var generateimage in createdashVM.PhotoList) { if (!generateimage.IsImage()) { ModelState.AddModelError("Photo", "You can chose only image format"); return(View()); } if (!generateimage.CheckSize(2)) { ModelState.AddModelError("Photo", "You can chose only small 2 MB"); return(View()); } string createdImage = await generateimage.CopyImage(_env.WebRootPath, "product"); Image image = new Image() { PathImage = createdImage, ProductId = product.Id, Active = null, }; await _remindb.Images.AddAsync(image); await _remindb.SaveChangesAsync(); } ; } } catch (Exception ex) { return(RedirectToAction(nameof(Index))); } return(RedirectToAction("Index", "ProductGenerate")); }
public async Task <IActionResult> Update([Bind(include: "")] int id, PoductUpdateViewModel updateProductViewModel) { Product _dbproduct = await _remindb.Products.FirstOrDefaultAsync(p => p.Id == id); if (_dbproduct == null) { return(NotFound()); } try { if (ModelState["Name"].ValidationState == ModelValidationState.Invalid || ModelState["Title"].ValidationState == ModelValidationState.Invalid || ModelState["Description"].ValidationState == ModelValidationState.Invalid || ModelState["Count"].ValidationState == ModelValidationState.Invalid || ModelState["Price"].ValidationState == ModelValidationState.Invalid || //ModelState["DiscountProduct"].ValidationState == ModelValidationState.Invalid || //ModelState["ProductDedline"].ValidationState == ModelValidationState.Invalid || ModelState["MostView"].ValidationState == ModelValidationState.Invalid) { return(RedirectToAction(nameof(Index))); } if (updateProductViewModel.DiscountProduct == 0) { updateProductViewModel.ProductDedline = null; } ; #region _dbproduct.Name = updateProductViewModel.Name; _dbproduct.Title = updateProductViewModel.Title; _dbproduct.Description = updateProductViewModel.Description; _dbproduct.Price = updateProductViewModel.Price; _dbproduct.Count = updateProductViewModel.Count; _dbproduct.DiscountProduct = updateProductViewModel.DiscountProduct; _dbproduct.ProductDedline = updateProductViewModel.ProductDedline; _dbproduct.MostView = updateProductViewModel.MostView; _dbproduct.Active = null; #endregion if (!_remindb.CategoryMarkas.Any(d => d.CategoryId == updateProductViewModel.CategoryId && d.MarkaId == updateProductViewModel.MarkaId)) { CategoryMarka categoryMarka = new CategoryMarka() { CategoryId = updateProductViewModel.CategoryId, MarkaId = updateProductViewModel.MarkaId, }; await _remindb.CategoryMarkas.AddAsync(categoryMarka); await _remindb.SaveChangesAsync(); updateProductViewModel.CategoryMarkaId = categoryMarka.Id; _dbproduct.CategoryMarkaId = updateProductViewModel.CategoryMarkaId; } else { updateProductViewModel.CategoryMarkaId = _remindb.CategoryMarkas.FirstOrDefault(cm => cm.CategoryId == updateProductViewModel.CategoryId && cm.MarkaId == updateProductViewModel.MarkaId).Id; _dbproduct.CategoryMarkaId = updateProductViewModel.CategoryMarkaId; }; if (updateProductViewModel.ChangePhotoList != null) { foreach (var generateimage in updateProductViewModel.ChangePhotoList) { if (!generateimage.IsImage()) { ModelState.AddModelError("Photo", "You can chose only image format"); return(View()); } if (!generateimage.CheckSize(2)) { ModelState.AddModelError("Photo", "You can chose only small 2 MB"); return(View()); } string createdImage = await generateimage.CopyImage(_env.WebRootPath, "product"); Image image = new Image() { PathImage = createdImage, ProductId = _dbproduct.Id,// imageler id product saxlanaq;lll Active = null, }; await _remindb.Images.AddAsync(image); await _remindb.SaveChangesAsync(); } ; } await _remindb.SaveChangesAsync(); } catch (Exception ex) { await _remindb.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(RedirectToAction(nameof(Index))); }