public ActionResult EditVehicle(int id) { var model = new EditVehicleViewModel(); model.Vehicle = _carDealer.GetVehicleById(id); model.SetMakeType(_carDealer.GetAllCarMakes()); model.SetModelType(_carDealer.GetAllCarModels()); model.SetBodyType(_carDealer.GetBodyStyles()); model.SetVehicleType(_carDealer.GetVehicleType()); model.SetTransmissionType(_carDealer.GetTransmissions()); model.SetColorOptions(); model.SetInteriorTypes(); //throw new Exception(); return(View(model)); }
public ActionResult EditVehicle(EditVehicleViewModel model) { if (ModelState.IsValid) { var localhost = "http://localhost:50822/Images/"; //if (string.IsNullOrEmpty(model.Vehicle.Image) && model.VehicleImage == null) //{ // ModelState.AddModelError("VehicleImage", "Image required for vehicle!"); //} if (string.IsNullOrEmpty(model.Vehicle.Description)) { ModelState.AddModelError("Description", "Vehicle must have some sort of description."); } if (model.Vehicle.VIN == null || model.Vehicle.VIN.Length != 17) { ModelState.AddModelError("VIN", "Vehicle VIN numbers must be 17 digits long!"); } if (model.Vehicle.MSRP < model.Vehicle.SalePrice) { ModelState.AddModelError("SalePrice", "Sale Price can not be larger than MSRP!"); } if (model.VehicleImage != null && model.VehicleImage.ContentLength > 0) { var savePath = Server.MapPath("~/Images"); string fileName = Path.GetFileName(model.VehicleImage.FileName); string extension = Path.GetExtension(model.VehicleImage.FileName); var filePath = Path.Combine(savePath, fileName + extension); model.VehicleImage.SaveAs(filePath); model.Vehicle.ImageUrl = localhost + model.VehicleImage.FileName; if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); model.VehicleImage.SaveAs(filePath); } //int counter = 1; //while (System.IO.File.Exists(filePath)) //{ // filePath = Path.Combine(savePath, fileName + counter.ToString() + extension); // counter++; //} } _carDealer.EditVehicle(model); } else { model.SetMakeType(_carDealer.GetAllCarMakes()); model.SetModelType(_carDealer.GetAllCarModels()); model.SetBodyType(_carDealer.GetBodyStyles()); model.SetVehicleType(_carDealer.GetVehicleType()); model.SetTransmissionType(_carDealer.GetTransmissions()); model.SetColorOptions(); model.SetInteriorTypes(); return(View(model)); } return(RedirectToAction("Vehicles")); }