Home > Enterprise >  Passing form data to a model when using multiple models in the same view
Passing form data to a model when using multiple models in the same view

Time:11-16

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?

CodePudding user response:

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

  • Related