public ActionResult Create(CreateCarViewModel model) { if (ModelState.IsValid) { if (model.Photo != null && model.Photo.ContentLength > 0) { var savepath = Server.MapPath("~/Images"); string fileName = Path.GetFileNameWithoutExtension(model.Photo.FileName); string extension = Path.GetExtension(model.Photo.FileName); var filePath = Path.Combine(savepath, fileName + extension); int counter = 1; while (System.IO.File.Exists(filePath)) { filePath = Path.Combine(savepath, fileName + counter.ToString() + extension); counter++; } model.Photo.SaveAs(filePath); model.GottenPicture = Path.GetFileName(filePath); } carsRepository.AddCars(new Cars() { BodyStyleId = model.BodyStyleId, CarTypeId = model.TypeId, CarModelId = model.ModelId, CarYear = model.Year, ColorId = model.ColorId, Discription = model.Discription, InteriorColorId = model.InteriorId, TransmissionId = model.TransmissionId, Mileage = model.Mileage, MSRP = model.MSRP, SalesPrice = model.Price, Sold = false, Special = false, Vin = model.Vin, Photo = model.GottenPicture }); return(RedirectToAction("Cars")); } else { return(View(new CreateCarViewModel() { BodyStyles = carsRepository.GetAllBodyStyle().BodyStyles.Select(m => new SelectListItem { Text = m.BodyStyleName, Value = m.BodyStyleId.ToString() }), Colors = carsRepository.GetAllColor().Colors.Select(c => new SelectListItem { Text = c.ColorName, Value = c.ColorId.ToString() }), Interiors = carsRepository.GetAllInteriorColors().InteriorColorss.Select(m => new SelectListItem { Text = m.InteriorColorName, Value = m.InteriorColorId.ToString() }), CarId = carsRepository.GetAllCars().Carss.Max(c => c.CarId) + 1, Makes = carsRepository.GetAllMake().Makes.Select(m => new SelectListItem { Text = m.MakeName, Value = m.MakeId.ToString() }), Models = carsRepository.GetAllCarModel().CarModels.Select(m => new SelectListItem { Text = m.CarModelName, Value = m.CarModelId.ToString() }), Transmissions = carsRepository.GetAllTransmission().Transmissions.Select(m => new SelectListItem { Text = m.TransmissionName, Value = m.TransmissionId.ToString() }), Types = carsRepository.GetAllCarType().CarTypes.Select(m => new SelectListItem { Text = m.CarTypeName, Value = m.CarTypeId.ToString() }) })); } }