public ActionResult EditVehicle(int Id) { var manager = ManagerFactory.Create(); var selectedVehicle = manager.GetVehicle(Id); var vm = new EditVehicleVM(); vm.SetSelectionsForEdit(selectedVehicle); vm.SetAllLists(manager); vm.VehicleId = selectedVehicle.Id; return(View(vm)); }
public ActionResult EditVehicle(EditVehicleVM vm) { { var manager = ManagerFactory.Create(); if (vm.Year < 2000 || vm.Year > DateTime.Now.Year + 1) { ModelState.AddModelError("", "Error: Vehicle year must be between 2000 and " + (DateTime.Now.Year + 1) + "."); } if (manager.GetCondition(vm.SelectedConditionId).Name == "New" && vm.Mileage > 1000) { ModelState.AddModelError("", "Error: If condition is set to new, mileage must be less than 1,000."); } if (manager.GetCondition(vm.SelectedConditionId).Name == "Used" && vm.Mileage <= 1000) { ModelState.AddModelError("", "Error: If condition is set to used, mileage must be greater than 1,000."); } if (vm.VIN == "") { ModelState.AddModelError("", "Error: VIN cannot be blank."); } if (vm.MSRP <= 0) { ModelState.AddModelError("", "Error: MSRP must be greater than 0."); } if (vm.SalePrice <= 0) { ModelState.AddModelError("", "Error: sale price must be greater than 0."); } if (vm.SalePrice > vm.MSRP) { ModelState.AddModelError("", "Error: Sale price must be lower than MSRP."); } if (vm.Description == "") { ModelState.AddModelError("", "Error: A description is required."); } if (!ModelState.IsValid) { vm.SetAllLists(manager); var vehicleToEdit = manager.GetVehicle(vm.VehicleId); vm.SetSelectionsForEdit(vehicleToEdit); return(View(vm)); } else { Vehicle toEdit = new Vehicle { Id = vm.VehicleId, BodyStyle = manager.GetBodyStyle(vm.SelectedStyleId), ConditionType = manager.GetCondition(vm.SelectedConditionId), Description = vm.Description, ExteriorColor = manager.GetColor(vm.SelectedColorId), InteriorColor = manager.GetColor(vm.SelectedColorId), Mileage = vm.Mileage, ModelType = manager.GetModel(vm.SelectedModelId), MSRP = vm.MSRP, SalePrice = vm.SalePrice, Trans = manager.GetTransmission(vm.SelectedTransId), VIN = vm.VIN, Year = vm.Year, IsFeatured = vm.IsFeatured, PicturePath = vm.PicturePath }; if (vm.Picture != null) { string dir = Server.MapPath("~/Images"); vm.Picture.SaveAs(Path.Combine(dir, "inventory-" + toEdit.Id.ToString() + ".jpg")); toEdit.PicturePath = "~/Images/" + "inventory-" + toEdit.Id.ToString() + ".jpg"; } manager.EditVehicle(toEdit); return(RedirectToAction("Vehicles")); } } }