Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@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?

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading