public ActionResult ProductEdit(ProductEditVM peVM, HttpPostedFileBase file) { if (ModelState.IsValid) { // product image file upload //if (file.ContentLength > 0) //{ // string fileName = "Product-" + peVM.ProductVariant.Image.ImageID.ToString() + "-mid"; // string path = Path.Combine(Server.MapPath("~/Contents/Site-Image"), fileName); // file.SaveAs(path); //} // Attach ? think later // try to save change without reading from DB again - ProductVariant & Button // How to update only necessary fields to DB , with out retrieving all the fields for single obj , with only using ButtonID // just retrieve from DB for now // TODO : Modify code to save Product + Button @ one DB request // productRepo.SaveProduct(peVM.ProductVariant.Product); // also saves product variant ? NO ! // ProductVariant update is not working form GSDA //// Option 1 //var prodVar = productRepo.ProductVariants // .Where(pv => pv.VariantID == peVM.ProductVariant.VariantID) // .Single(); //// Option 2 -call entity from context or Attach to context and modify properties ////productRepo.Context.ProductVariants.Attach(peVM.ProductVariant); ////peVM.ProductVariant.SKU = peVM.ProductVariant.SKU; ////peVM.ProductVariant.Name = peVM.ProductVariant.Name; //prodVar.Cost = peVM.ProductVariant.Cost; //prodVar.Description = peVM.ProductVariant.Description; //prodVar.Name = peVM.ProductVariant.Name; //prodVar.Note = peVM.ProductVariant.Note; //prodVar.Price = peVM.ProductVariant.Price; //productRepo.SaveProductVariant(prodVar); productRepo.SaveProductVariant(peVM.ProductVariant); if (!String.IsNullOrEmpty(peVM.SelectedButtonID)) { int selectedButtonID = Int32.Parse(peVM.SelectedButtonID); // == null ? 0 : Int32.Parse(peVM.SelectedButtonID); Button modifiedButton = buttonRepo.Buttons //.Include(b => b.Variant) .Where(b => b.ButtonID == selectedButtonID) .FirstOrDefault(); //modifiedButton.Variant = peVM.ProductVariant; modifiedButton.VariantID = peVM.ProductVariant.VariantID; buttonRepo.SaveButton(modifiedButton); } // Save Product + ProductVariant OR ProductVariant only ??? return View("TaskCompleted"); } else { return View(peVM); } }
public ActionResult ProductEdit(int VariantID = 1) { // Need dictionary to retrieve ButtonID as string since IQueryable cannot ! Dictionary<int, string> newButtons = buttonRepo.Buttons.ToDictionary(b => b.ButtonID, b => b.Name); ProductEditVM peVM = new ProductEditVM { ProductVariant = productRepo.ProductVariants //.Include("Product.Categories") .Include("Product") .Where(pv => pv.VariantID == VariantID) .FirstOrDefault() , Buttons = newButtons.Select(b => new SelectListItem { Value = b.Key.ToString(), Text = b.Value, Selected = false }).ToList() }; Button btn = buttonRepo.Buttons .Where(b => b.VariantID == VariantID) .FirstOrDefault(); //string btnID = buttonRepo.Buttons // .Where(b => b.VariantID == VariantID) // .FirstOrDefault() // .ButtonID.ToString(); if (btn != null) { peVM.Buttons .Where(b => b.Value == btn.ButtonID.ToString()) .FirstOrDefault() .Selected = true; } return View(peVM); }