title: FluentValidation RuleFor attribute of Foreign Key
body: <p>When I run the program I get a System.NullReferenceException on <code>x.Album.Title == "Disintegration"</code> in:</p>
  
  <pre><code>RuleFor(x =&gt; x.Contents)
              .NotNull()
              .When(x =&gt; x.Album.Title == "Disintegration");
  </code></pre>
  
  <p>How can I program this so that <code>Contents</code> is not accepted as Null when <code>Album.Title == "Disintegration</code>?</p>
  
  <p>Model</p>
  
  <pre><code>[Validator(typeof(ReviewValidation))]
  public class Review
  {
      public int ReviewID { get; set; }
  
      [Display(Name = "Album")]
      public int AlbumID { get; set; }
  
      public virtual Album Album { get; set; }
  
      public string Contents { get; set; }
  
      [DataType(DataType.EmailAddress)]
      public string ReviewerEmail { get; set; }
  }
  </code></pre>
  
  <p>Validation</p>
  
  <pre><code>public class ReviewValidation : AbstractValidator&lt;Review&gt;
  {
      public ReviewValidation()
      {
          RuleFor(x =&gt; x.Contents)
              .NotNull()
              .When(x =&gt; x.Album.Title == "Disintegration");
      }
  }
  </code></pre>
  
  <p>Controller </p>
  
  <pre><code>public class ReviewsController : Controller
  {
      private StoreContext db = new StoreContext();
      public ActionResult Create()
      {
          ViewBag.AlbumID = new SelectList(db.Albums, "AlbumID", "Title");
          return View();
      }
  
      [HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Create(Review review)
      {
          if (ModelState.IsValid)
          {
              db.Reviews.Add(review);
              db.SaveChanges();
              return RedirectToAction("Index");
          }
  
          ViewBag.AlbumID = new SelectList(db.Albums, "AlbumID", "Title", review.AlbumID);
          return View(review);
      }
  }
  </code></pre>
  
  <p>View</p>
  
  <pre><code>@model MVCMusicStore.Models.Review
  
  
  @using (Html.BeginForm()) 
  {
  @Html.AntiForgeryToken()
  
  &lt;div class="form-horizontal"&gt;
      &lt;h4&gt;Review&lt;/h4&gt;
      &lt;hr /&gt;
      @Html.ValidationSummary(true, "", new { @class = "text-danger" })
      &lt;div class="form-group"&gt;
          @Html.LabelFor(model =&gt; model.AlbumID, "AlbumID", htmlAttributes: new { @class = "control-label col-md-2" })
          &lt;div class="col-md-10"&gt;
              @Html.DropDownList("AlbumID", null, htmlAttributes: new { @class = "form-control" })
              @Html.ValidationMessageFor(model =&gt; model.AlbumID, "", new { @class = "text-danger" })
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="form-group"&gt;
          @Html.LabelFor(model =&gt; model.Contents, htmlAttributes: new { @class = "control-label col-md-2" })
          &lt;div class="col-md-10"&gt;
              @Html.EditorFor(model =&gt; model.Contents, new { htmlAttributes = new { @class = "form-control" } })
              @Html.ValidationMessageFor(model =&gt; model.Contents, "", new { @class = "text-danger" })
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="form-group"&gt;
          @Html.LabelFor(model =&gt; model.ReviewerEmail, htmlAttributes: new { @class = "control-label col-md-2" })
          &lt;div class="col-md-10"&gt;
              @Html.EditorFor(model =&gt; model.ReviewerEmail, new { htmlAttributes = new { @class = "form-control" } })
              @Html.ValidationMessageFor(model =&gt; model.ReviewerEmail, "", new { @class = "text-danger" })
          &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;
  }
  
  &lt;div&gt;
      @Html.ActionLink("Back to List", "Index")
  &lt;/div&gt;
  
  @section Scripts {
      @Scripts.Render("~/bundles/jqueryval")
  }
  </code></pre>