How would you pass form data to a model (to be used inside my controller) when I have multiple models in a single view?
I used this tutorial for multiple models in a view.
Index view and form (simplifed):
@using FlightBooker
@model dynamic
<form asp-controller="Home" asp-action="Search" method="post">
<div class="form-group">
<label for="fromAirport">Flying from:</label>
<select class="form-control">
@foreach (Airport airport in Model.Airports)
{
<option >@airport.AirportCode</option>
}
</select>
<div class="form-group">
<label for="toAirport">Flying to:</label>
<select class="form-control">
@foreach (Airport airport in Model.Airports)
{
<option>@airport.AirportCode</option>
}
</select>
</div>
<label for="fromAirport">Departure Date:</label>
<br />
<input type="date" / id="date" name="date">
<br />
<label for="fromAirport">No. passengers:</label>
<select class="form-control" id="passengers" name="passengers">
<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>
I was looking at this tutorial, this section: Strongly type model binding to view. I don't know how to apply this when I am using @model dynamic
.
Or would I be better creating a View Model as suggested here?