private void SaveImageMappings(ProductCreateOrUpdateModel model) { var imageMappings = new List <ProductImageMapping>(); if (model.ImageIds != null) { for (int i = 0; i < model.ImageIds.Count; i++) { // convert sort order to int int sortOrder = Convert.ToInt32(Math.Floor(Convert.ToDouble(model.ImageSortOrder[i]))); // check if image exist Guid imageId; if (Guid.TryParse(model.ImageIds[i], out imageId)) { // create mapping entity var imageMapping = new ProductImageMapping { Id = Guid.NewGuid(), ProductId = model.Id, ImageId = Guid.Parse(model.ImageIds[i]), SortOrder = sortOrder, Position = i }; imageMappings.Add(imageMapping); } } } // save to database _imageManagerService.DeleteAllProductImageMappings(model.Id); _imageManagerService.InsertProductImageMappings(imageMappings); }
public ActionResult DeleteConfirmed(int id) { ProductImageMapping productImageMapping = db.ProductImageMappings.Find(id); db.ProductImageMappings.Remove(productImageMapping); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Edit([Bind(Include = "ID,ImageNumber,ProductID,ProductImageID")] ProductImageMapping productImageMapping) { if (ModelState.IsValid) { db.Entry(productImageMapping).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ProductID = new SelectList(db.Products, "ID", "Name", productImageMapping.ProductID); ViewBag.ProductImageID = new SelectList(db.ProductImages, "ID", "FileName", productImageMapping.ProductImageID); return(View(productImageMapping)); }
// GET: ProductImageMappings/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } ProductImageMapping productImageMapping = db.ProductImageMappings.Find(id); if (productImageMapping == null) { return(HttpNotFound()); } return(View(productImageMapping)); }
// GET: ProductImageMappings/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } ProductImageMapping productImageMapping = db.ProductImageMappings.Find(id); if (productImageMapping == null) { return(HttpNotFound()); } ViewBag.ProductID = new SelectList(db.Products, "ID", "Name", productImageMapping.ProductID); ViewBag.ProductImageID = new SelectList(db.ProductImages, "ID", "FileName", productImageMapping.ProductImageID); return(View(productImageMapping)); }
public ActionResult Create(ProductViewModel viewModel) { var product = new Product(); product.Name = viewModel.Name; product.Category = _categoryRepo.Find(viewModel.CategoryID); product.CategoryID = viewModel.CategoryID; product.Description = viewModel.Description; product.Price = viewModel.Price; product.ProductImageMappings = new List <ProductImageMapping>(); string[] productImages = viewModel.ProductImages.Where(pi => !string.IsNullOrWhiteSpace(pi)).ToArray(); for (int i = 0; i < productImages.Length; i++) { var prodImageMapping = new ProductImageMapping(); prodImageMapping.ImageNumber = i; prodImageMapping.ProductImage = _productImageRepo.Find(int.Parse(productImages[i])); product.ProductImageMappings.Add(prodImageMapping); } if (ModelState.IsValid) { _productRepo.Add(product); _unitOfWork.Save(); return(RedirectToAction("Index")); } var allCategories = _categoryRepo.GetTable(); viewModel.CategoryList = new SelectList(allCategories, "ID", "Name", product.CategoryID); viewModel.ImageLists = new List <SelectList>(); for (int i = 0; i < Constants.NumberOfProductImages; i++) { viewModel.ImageLists.Add(new SelectList(_productImageRepo.GetTable(), "ID", "FileName", viewModel.ProductImages[i])); } ViewBag.CategoryID = new SelectList(allCategories, "ID", "Name", product.CategoryID); return(View(product)); }