title: MVC5 Razor NullReferenceException in Model
body: <p>For some reason I'm getting a NullReferenceException whenever I try to access my model.</p>
  
  <p>Here is the code from my controller:</p>
  
  <pre><code>public async Task&lt;ActionResult&gt; Bar(string fooSlug, int barId)
  {
      var foo = await mediaService.GetFoo(fooSlug);
      var bar = await barService.GetBarFromFooByTitleId(foo.TitleId, barId);
      var viewModel = new ViewModels.BarViewModel(foo, bar);
      return View(viewModel);
  }
  </code></pre>
  
  <p>Code from the ViewModel:</p>
  
  <pre><code>public class BarViewModel
  {
      public Models.Sub.FooDetails Foo{ get; set; }
      public Models.Sub.BarDetails Bar { get; set; }
  
      public BarViewModel(Models.Sub.FooDetails foo, Models.Sub.BarDetails bar) 
      {
          this.Foo = foo;
          this.Bar = bar;
      }
  }
  </code></pre>
  
  <p>And my View:</p>
  
  <pre><code>@model FooBar.ViewModels.BarViewModel
  
  @{
      ViewBag.Title = "Bar";
  }
  
  &lt;h2&gt;@Model.Bar.Name&lt;/h2&gt;
  </code></pre>
  
  <p>It keeps returning a NullReferenceException When I try to use it inside the h2 tag. I've debugged it and the .Name property has the correct value, but when I press continue it will just throw the error.</p>
  
  <p>Does anyone have a solution for this problem?</p>