1

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?

Christian
  • 391
  • 3
  • 12
  • 1
    make a ViewModel Called "SearchViewModel", add properties with the name same as the html names of input fields for exmaple public class SearchViewModel { public datetime date {get;set;} } when u put it as a parameter of the action called search it will automatically bind to the values. Note : add the values of the select inside of the html – IbraHim M. Nada Nov 15 '21 at 11:20

1 Answers1

2

Okay, I'm gonna tell you what to do here.....

the model is used to render data from controller to the view.

if you want to pass data from view to controller there are some simpler ways...

step1:

set all of the html input/select elements the value of Name

Example :

  <select class="form-control" name"AirportsDdl">
            @foreach (Airport airport in Model.Airports)
            {
                <option value="@airport.AirportCode">@airport.AirportCode</option>
            }

        </select>

<input type="date" / id="date" name="date">

Notice that the Select must have a value

Step2

then you make a ViewModel like this

public class SearchViewModel
{
public datetime date {get;set;}
public int AirportsDdl {get;set;}
}

step3:

then in your action :

ActionResult Search(SearchViewModel sreachData)
{
//put your filter here
return view();
}

then you all set

IbraHim M. Nada
  • 693
  • 6
  • 26
  • I had done step 2 and 3 already, it was step 1 I was missing. Seeing it laid out like this has helped massively, thanks :) – Christian Nov 15 '21 at 11:40