title: ViewModel Error : Object reference not set to an instance of an object
body: <p>I cannot understand why I am getting this error, could you please help me find what I am doing wrong ?</p>
  
  <p>What i am trying to achieve with this ViewModel is because i have many records with the same CartId to use it as a parent and booking.Item.ItemName as a child.</p>
  
  <p>Can you please advise if the way i am trying to do it is correct?</p>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>public ActionResult MyBookings()
  {
  
      var myBookingList = SystemDB.Carts.Where(s =&gt; s.Booking.Login == strLoginName).OrderByDescending(s =&gt; s.RecordId).Take(10);
  
  
      List&lt;MyBookingsViewModel&gt; result = new List&lt;MyBookingsViewModel&gt;();
      foreach (var item in myBookingList)
      {
          MyBookingsViewModel model = new MyBookingsViewModel();
  
          model.Carts.RecordId = item.RecordId;
          model.Carts.CartId = item.CartId;
          model.Carts.Booking.Item.ItemName = item.Booking.Item.ItemName;
          model.Carts.Booking.UserFullName = item.Booking.UserFullName;
          model.Carts.Booking.RequestDate = item.Booking.RequestDate;
          model.Carts.StatusCode.StatusCodeName = item.StatusCode.StatusCodeName;
          result.Add(model);
      }
  
  
      return View(result);
  }
  </code></pre>
  
  <p><strong>Model:</strong></p>
  
  <pre><code>public class MyBookingsViewModel
  {
      public Cart Carts { get; set; }
      public List&lt;Booking&gt; Bookings { get; set; }
  }
  
  public class Cart
  {
      [Key]
      public int RecordId { get; set; }
      public string CartId { get; set; }       
      public int Count { get; set; }
  
  
  
      [ForeignKey("Booking")]
      public int BookingId { get; set; }
      public virtual Booking Booking { get; set; }
  
      [ForeignKey("StatusCode")]
      public int Stage { get; set; }
      public virtual StatusCode StatusCode { get; set; }
  
      public Cart()
      {
          this.Booking = new List&lt;Booking&gt;();
      }
  }
  
  public class Booking
  {
       [DisplayName("BookingId")]
      public int BookingId { get; set; }
  
      [ForeignKey("Item")]
      public int ItemId { get; set; }
      public virtual Item Item { get; set; }
  
      [DisplayName("Name")]
      public string UserFullName { get; set; }
  
  
      [DateStart]
      [DisplayFormat(DataFormatString = "{0:dd/MM/yy hh:mm tt}", ApplyFormatInEditMode = true)]
      public DateTime StartDate { get; set; }
  
      [DateEnd(DateStartProperty = "StartDate")]
      [DisplayFormat(DataFormatString = "{0:dd/MM/yy hh:mm tt}", ApplyFormatInEditMode = true)]
      public DateTime EndDate { get; set; }
  
      [DataType(DataType.Date)]
      [DisplayFormat(DataFormatString = "{0:d MMM yyyy}", ApplyFormatInEditMode = true)]
      public System.DateTime RequestDate { get; set; }
  
      [DisplayName("Login")]
      public string Login { get; set; }
  
      [DisplayName("NetworkID")]
      public string NetworkID { get; set; }
  
      [DisplayName("Comments")]
      public string Comments { get; set; }
  
  }
  </code></pre>
  
  <p><strong>View:</strong></p>
  
  <pre><code>@foreach (var item in Model) {
      &lt;tr&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.Carts.RecordId)
          &lt;/td&gt;
          &lt;td&gt;
              &lt;a href="@Url.Action("ItemDetails", new {id=item.Carts.BookingId })" &gt;
              @Html.DisplayFor(modelItem =&gt; item.Carts.CartId)
                  &lt;/a&gt;
              @foreach(var booking in item.Bookings)
     {
       &lt;li&gt;@booking.Item.ItemName&lt;/li&gt;
     }
          &lt;/td&gt;
  &lt;/tr&gt;
  }
  </code></pre>