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