public ActionResult New(int id) { Vehicle myVehicle = new Vehicle() { Id = -1 }; var vehicles = _context.Vehicles.ToList(); foreach (var vehicle in vehicles) { if (vehicle.Id == id) { myVehicle = vehicle; } } if (myVehicle.Id == -1) { return(HttpNotFound()); } FormRequestModel model = new FormRequestModel() { vehicle = myVehicle, request = new Request() { VehicleId = id } }; return(View(model)); }
public ActionResult Create(Request request) { if (!ModelState.IsValid) { var myVehicle = _context.Vehicles.Single(c => c.Id == request.VehicleId); FormRequestModel model = new FormRequestModel() { vehicle = myVehicle, request = new Request() { VehicleId = myVehicle.Id, IsValid = false } }; return(View("New", model)); } _context.Requests.Add(request); _context.SaveChanges(); return(RedirectToAction("Index", "Vehicle")); }