title: How to handle null child Entities in MVC Razor
body: <p>I have an MVC razor view that iterates over an Orders collection.  Each order has a Customer, which can be null.</p>
  
  <p>Trouble is, I get a null reference exception when this is the case.</p>
  
  <pre><code>@foreach (var item in Model) {
  &lt;tr&gt;
          &lt;td&gt;
          @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
          @Html.ActionLink("Delete", "Delete", new { id=item.ID })
      &lt;/td&gt;
      &lt;td&gt;
          @item.Number
      &lt;/td&gt;
      &lt;td&gt;
          @String.Format("{0:g}", item.ReceivedDate)
      &lt;/td&gt;
      &lt;td&gt;
          @item.Customer.Name
      &lt;/td&gt;
  </code></pre>
  
  <p>@item.Customer.Name blows up when item.Customer is null (as you'd expect).</p>
  
  <p>This must be an easy question but haven't been able to find the answer!</p>
  
  <p>What's the best way to deal with this, without setting up a ViewModel ?</p>
  
  <p>Thanks
  Duncan</p>