Foreach loop Null model in Razor view

497 views Asked by At

I'm trying to Add values in table(Flight) which have one to many relation with table(Flight classes) for view of flightClasses i'm using foreach loop but it comes with null model i can't understand how to do it please help me

Flight Table will have one Row against which i want to add multiple rows in the FlightClasses Table Flight Modle

public class FlightModel
    {
        public int Id { get; set; }
        public string FlightName { get; set; }
        public string FlightNo { get; set; }
        public string OriginCity { get; set; }
        public string DestinationCity { get; set; }
        public DateTime Departure { get; set; }
        public DateTime Arrival { get; set; }
        public virtual ICollection<FlightClassesModel> FlightClasses { get; set; }
    }

FlightClasses Model

public class FlightClassesModel
    {

        public int FlightClassesModelId { get; set; }
        public type Class { get; set; }
        public int AvailableSeats { get; set; }
        public double Price { get; set; }
        public virtual FlightModel Flight { get; set; }

        public int? FlightId { get; set; }
    }
    public enum type
    {
        Business_Class,
        First_Class,
        Club_Class
    }

View

@model Airline.Models.FlightModel

@{
    ViewData["Title"] = "Create";
}

<h2>Create</h2>

<h4>FlightModel</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="FlightName" class="control-label"></label>
                <input asp-for="FlightName" class="form-control" />
                <span asp-validation-for="FlightName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="FlightNo" class="control-label"></label>
                <input asp-for="FlightNo" class="form-control" />
                <span asp-validation-for="FlightNo" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="OriginCity" class="control-label"></label>
                <input asp-for="OriginCity" class="form-control" />
                <span asp-validation-for="OriginCity" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="DestinationCity" class="control-label"></label>
                <input asp-for="DestinationCity" class="form-control" />
                <span asp-validation-for="DestinationCity" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Departure" class="control-label"></label>
                <input asp-for="Departure" class="form-control" />
                <span asp-validation-for="Departure" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Arrival" class="control-label"></label>
                <input asp-for="Arrival" class="form-control" />
                <span asp-validation-for="Arrival" class="text-danger"></span>
            </div>
            @foreach (var flightClass in Model.FlightClasses)
            {
                <div class="form-group">
                    <label asp-for="@flightClass.Class" class="control-label"></label>
                    <select asp-for="@flightClass.Class" asp-items="Html.GetEnumSelectList<type>()"></select>
                    <span asp-validation-for="@flightClass.Class" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="@flightClass.AvailableSeats" class="control-label"></label>
                    <input asp-for="@flightClass.AvailableSeats" class="form-control" />
                    <span asp-validation-for="@flightClass.AvailableSeats" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="@flightClass.Price" class="control-label"></label>
                    <input asp-for="@flightClass.Price" class="form-control" />
                    <span asp-validation-for="@flightClass.Price" class="text-danger"></span>
                </div>
            }

            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

Controller

public IActionResult Create()
        {
            return View();
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create(FlightModel flightModel)
        {
            if (ModelState.IsValid)
            {
                _context.Add(flightModel);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(flightModel);
        }
0

There are 0 answers