public IActionResult Create(VehicleCreateModel model) { foreach (VehicleType vehicleType in model.VehicleTypes) { if (vehicleTypeRepository.Exists(vehicleType)) { ModelState.AddModelError(string.Empty, "VehicleType already exists"); } if (ModelState.IsValid) { vehicleType.Id = vehicleTypeRepository.Create(vehicleType); } } foreach (Vehicle vehicle in model.Vehicles) { if (vehicleRepository.Exists(vehicle)) { ModelState.AddModelError(string.Empty, "Vehicle already exists"); } if (ModelState.IsValid) { VehicleType previouslyStoredType = vehicleTypeRepository.GetByTitle(vehicle.VehicleType.Title); if (previouslyStoredType != null) { vehicle.VehicleTypeId = previouslyStoredType.Id; vehicleRepository.Create(vehicle); } } } if (ModelState.IsValid) { return(RedirectToAction("Index")); } ViewData["CompanyId"] = new SelectList(companyRepository.GetAll(), "Id", "Title"); ViewData["VehicleTypeId"] = new SelectList(companyRepository.GetAll(), "Id", "Title"); return(View(model)); }