title: Asp .Net Core 3.1 Razor Page: Complex model binding
body: <p>I am struggeling with data binding and Razor pages</p>
  <p>I was not able to find a solution. Propably I am asking not the right questions.
  So I hope you can help me out to point me to the right direction.</p>
  <p>I want a page to create bookings to collect all necessary data to post it to a web api when everything is done.</p>
  <p>On that page I want to collect necessary data for the wooking and want to add x Booking Details to the Booking.</p>
  <p><strong>Datamodel</strong></p>
  <pre><code>public class Booking
  {
     [Key]
     public int Id { get; set; }
  
     public string Reference { get; set; }
  
     public List&lt;BookingDetail&gt; BookingDetails { get; set; }
  }
  public class BookingDetail
  {
     [Key]
     public int Id { get; set; }
  
     public int Pieces { get; set; }
  
     public string Marks { get; set; }
  }
  </code></pre>
  <p>What I would like to to is to create a create page for a booking.
  So far I followed this instructions, which is working <a href="https://www.learnrazorpages.com/razor-pages/model-binding#binding-related-collections-to-a-complex-object" rel="nofollow noreferrer">Link</a></p>
  <p>This missing part is that I would like to add new Booking Details as well on the same Razor page.</p>
  <p>So I want to see a list of all added booking Details and I want to have textboxes to add new booking Details.</p>
  <p>My idea was to do the following, but I am receiving a <code>System.NullReferenceException</code> as Booking is null:</p>
  <pre><code>@page
  @model Onlinebooking.Web.Pages.BookingModel
  @{
      ViewData[&quot;Title&quot;] = &quot;Booking&quot;;
  }
  
  &lt;h1&gt;Booking4&lt;/h1&gt;
  &lt;form method=&quot;post&quot;&gt;
      Reference: &lt;Input asp-for=&quot;Booking.Reference&quot; /&gt;&lt;br /&gt;
      Pieces: &lt;input asp-for=&quot;Booking.BookingDetails[0].Pieces&quot; /&gt;&lt;br /&gt;
      Marks: &lt;input asp-for=&quot;Booking.BookingDetails[0].Marks&quot; /&gt;&lt;br /&gt;
      &lt;br /&gt;
      &lt;input type=&quot;submit&quot; value=&quot;submit&quot; /&gt;
  &lt;/form&gt;
  
  &lt;form method=&quot;post&quot; asp-page-handler=&quot;DeleteBookingDetails&quot;&gt;
      @for (var i = 0; i &lt; @Model.Booking.BookingDetails.Count(); i++)
      {
          @Html.LabelFor(x =&gt; x.Booking.BookingDetails[i].Pieces)
          @Html.LabelFor(x =&gt; x.Booking.BookingDetails[i].Marks)
      }
  &lt;/form&gt;
  </code></pre>
  <p>Is it even possible to do all that on one page or should I go in a different direction?</p>
  <p>Thanks for your help!</p>