I have a Search Form which goes to my Search() action method within my Home Controller. and returns View("Index")
Search() method:
        public IActionResult Search(SearchQuery FormData)
        {
            List<Flight> flights = new List<Flight>();
            flights = (from flight in _context.Flights
                       where flight.FlightDateTime.Date == FormData.PreferredFlightTime.Date
                       && flight.ArrivalAirportId == FormData.ArrivalAirportId
                       && flight.DepartureAirportId == FormData.DepartureAirportId
                       select flight).ToList();
            ViewBag.FlightSearchResults = flights;
            return View("Index");
        }
Upon the initial load of the homepage it works fine when loading my Index() method and returning the view.
        public IActionResult Index()
        {
            HomeViewModel mymodel = new HomeViewModel();
            mymodel.Airports = GetAirports();
 
            return View(mymodel);
        }
However once the search form is submitted and the Search() action method is called, when the method returns return View("Index"); the page will not load due System.NullReferenceException: 'Object reference not set to an instance of an object.'
The above exception is thrown when it reaches @foreach (Airport airport in Model.Airports)
Search form:
@using FlightBooker;
@model FlightBooker.Models.HomeViewModel;
<form asp-controller="Home" asp-action="Search" method="post">
                <div class="form-group">
                    <label for="fromAirport">Flying from:</label>
                    <select name=" {DepartureAirportId" class="form-control">
                        @foreach (Airport airport in Model.Airports)
                        {
                            <option value="@airport.AirportId">@airport.AirportCode</option>
                        }
                    </select>
                    <div class="form-group">
                        <label for="toAirport">Flying to:</label>
                        <select name="ArrivalAirportId" class="form-control">
                            @foreach (Airport airport in Model.Airports)
                            {
                                <option value="@airport.AirportId">@airport.AirportCode</option>
                            }
                        </select>
                    </div>
                    <label for="fromAirport">Departure Date:</label>
                    <br />
                    <input type="date" / id="date" name="PreferredFlightTime">
                    <br />
                    <label for="fromAirport">No. passengers:</label>
                    <select class="form-control" name="TotalPassengers">
                        <option>1</option>
                        <option>2</option>
                        <option>3</option>
                        <option>4</option>
                    </select>
                    <button type="submit" class="btn btn-primary mt-3">Search Flights</button>
                </div>
            </form>
