Hi all!
I am sorry if im asking same thing for the nth time but i can not find the answer to my question...
I have problem adding partial views...
I have One-to-Many relation in the database tables.
My Model is:
using System;
using System.Collections.Generic;
public partial class Job
{
public Job()
{
this.Flights = new HashSet<Flight>();
}
public int JobID { get; set; }
public string JobNumber { get; set; }
public int ClientID { get; set; }
public virtual Client Client { get; set; }
public virtual ICollection<Flight> Flights { get; set; }
}
My Controller (Create):
/ GET: /Jobs/Create
public ActionResult Create()
{
ViewBag.ClientID = new SelectList(db.Clients, "ClientID", "ClientName");
return View();
}
And View (for create Job):
@model AOG.Models.Job
<div class="form-horizontal">
<h4>Job</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.JobNumber, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.JobNumber)
@Html.ValidationMessageFor(model => model.JobNumber)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ClientID, "ClientID", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("ClientID", String.Empty)
@Html.ValidationMessageFor(model => model.ClientID)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
Now if i add partial view (Generated Partial List View) On Create Job Page:
@html.Partial("_PartialFlights");
I get an error: Object reference not set to an instance of an object on
Line 23: @foreach (var item in Model) {
in my partial view. This is the code:
@model IEnumerable<AOG.Models.Flight>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.FlightName)
</th>
<th>
@Html.DisplayNameFor(model => model.FlightETD)
</th>
<th>
@Html.DisplayNameFor(model => model.FlightETA)
</th>
<th>
@Html.DisplayNameFor(model => model.Job.JobNumber)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.FlightName)
</td>
<td>
@Html.DisplayFor(modelItem => item.FlightETD)
</td>
<td>
@Html.DisplayFor(modelItem => item.FlightETA)
</td>
<td>
@Html.DisplayFor(modelItem => item.Job.JobNumber)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.FlightID }) |
@Html.ActionLink("Details", "Details", new { id=item.FlightID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.FlightID })
</td>
</tr>
}
</table>
How ever If i put this in the details view:
@foreach (var item in Model.Flights)
{
<div>
<table>
<tr><th>Flight</th><th>ETD</th><th>ETA</th></tr>
<tr>
<td>@Html.DisplayFor(modelItem => item.FlightName)</td>
<td>@Html.DisplayFor(modelItem => item.FlightETD)</td>
<td>@Html.DisplayFor(modelItem => item.FlightETA)</td>
</tr></table>
</div>
}
It shows everything I need, but i want to learn how to do it with the partial views.
Thank you all so much!