public ActionResult AddVehicle() { var vm = new AddVehicleVM(); vm.SetAllLists(ManagerFactory.Create()); return(View(vm)); }
public ActionResult AddVehicle(AddVehicleVM 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); return(View(vm)); } else { Vehicle toAdd = new Vehicle { 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 = false //picture path }; var saved = manager.AddVehicle(toAdd); string dir = Server.MapPath("~/Images"); vm.Picture.SaveAs(Path.Combine(dir, "inventory-" + saved.Id.ToString() + ".jpg")); saved.PicturePath = "~/Images/" + "inventory-" + saved.Id.ToString() + ".jpg"; manager.EditVehicle(saved); return(RedirectToAction("Vehicles")); } }