public ActionResult AddNew(PartsProductsViewModel partsProductsViewModel) { try { if (ModelState.IsValid) { //VehicleVersionRepository vehicleVersionRepository = new VehicleVersionRepository(new AutoSolutionContext()); //bool IsExist = vehicleVersionRepository.isExist(vehicleVersionViewModel.VehicleVersionName, vehicleVersionViewModel.SelectedModel); //if (!IsExist) //{ PartsProduct partsProduct = new PartsProduct(); var ImageIds = partsProductsViewModel.PictureIDs.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(ID => int.Parse(ID)).ToList(); partsProduct.PartProductImages = new List <PartProductImages>(); foreach (var PicId in ImageIds) { PartProductImages partProductImages = new PartProductImages() { ImageId = PicId }; partsProduct.PartProductImages.Add(partProductImages); } partsProduct.AddedDate = DateTime.Now; partsProduct.EndYear = Convert.ToInt32(partsProductsViewModel.EndYear); partsProduct.startYear = Convert.ToInt32(partsProductsViewModel.startYear); partsProduct.UnitPrice = partsProductsViewModel.UnitPrice; partsProduct.ShortDescription = partsProductsViewModel.ShortDescription; partsProduct.LongDescription = partsProductsViewModel.LongDescription; partsProduct.PartsProductName = partsProductsViewModel.PartsProductName; partsProduct.VehicleManufacturerId = partsProductsViewModel.SelectedManufacturer; partsProduct.PartsProductsSubCategoryId = Convert.ToInt32(partsProductsViewModel.SelectedPartsProductSubCategory); partsProduct.VehicleModelId = partsProductsViewModel.SelectedModel; //partsProduct.PartsProductManufacturerId = Convert.ToInt32(partsProductsViewModel.SelectedPartProductManufacturer); _unitOfWork.PartsProducts.Add(partsProduct); _unitOfWork.Complete(); _unitOfWork.Dispose(); return(RedirectToAction("GetPartsProduct")); //} //else //{ // return RedirectToAction("GetVehicleVersion"); //} } } catch (Exception) { throw; } return(View()); }
public ActionResult Edit(PartsProductsViewModel partsProductsViewModel) { try { // PartsProduct partsProduct = new PartsProduct(); var ImageIds = partsProductsViewModel.PictureIDs.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(ID => int.Parse(ID)).ToList(); if (partsProductsViewModel == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } PartsProduct partsProduct = new PartsProduct(); partsProduct = _unitOfWork.PartsProducts.GetByID(partsProductsViewModel.PartsProductId); AutoSolutionContext autoSolutionContext = new AutoSolutionContext(); var partimagesDeleted = autoSolutionContext.PartProductImages.Where(x => x.PartsProductId == partsProduct.PartsProductId).ToList(); foreach (var item in partimagesDeleted) { autoSolutionContext.PartProductImages.Remove(item); // autoSolutionContext.Images.Remove(item.Image); } //foreach(var item in ImageIds) // { // var iamgeItem= autoSolutionContext.Images.Where(x => x.ImageId == item).FirstOrDefault(); // autoSolutionContext.Images.Remove(iamgeItem); // } autoSolutionContext.SaveChanges(); partsProduct.PartProductImages = new List <PartProductImages>(); foreach (var PicId in ImageIds) { PartProductImages partProductImages = new PartProductImages() { ImageId = PicId }; partsProduct.PartProductImages.Add(partProductImages); } //vehicleVersion.VehicleVersionId = vehicleVersionViewModel.VehicleVersionId; partsProduct.AddedDate = DateTime.Now; partsProduct.EndYear = Convert.ToInt32(partsProductsViewModel.EndYear); partsProduct.startYear = Convert.ToInt32(partsProductsViewModel.startYear); partsProduct.UnitPrice = partsProductsViewModel.UnitPrice; partsProduct.ShortDescription = partsProductsViewModel.ShortDescription; partsProduct.LongDescription = partsProductsViewModel.LongDescription; partsProduct.PartsProductName = partsProductsViewModel.PartsProductName; partsProduct.VehicleManufacturerId = Convert.ToInt32(partsProductsViewModel.SelectedManufacturer); partsProduct.PartsProductsSubCategoryId = Convert.ToInt32(partsProductsViewModel.SelectedPartsProductSubCategory); partsProduct.VehicleModelId = Convert.ToInt32(partsProductsViewModel.SelectedModel); partsProduct.PartsProductManufacturerId = Convert.ToInt32(partsProductsViewModel.SelectedPartProductManufacturer); _unitOfWork.PartsProducts.Update(partsProduct); _unitOfWork.Complete(); _unitOfWork.Dispose(); } catch (Exception) { throw; } return(RedirectToAction("GetPartsProduct")); }