title: MVC - Handling null values (NullReferenceException) in DropDownListFor
body: <p>I'm trying to customize Identity (2.1) using MVC5 and EF6.1. I've added a CountryId to the Identity users table and a whole separate Country table (CountryId, CountryName) with a FK constraint on CountryId. I'm trying to do a drop down list that selects the users country if the user has already selected it in the past (from the CountryId in the users table). However, if it hasn't been selected, the value will be null.</p>
  
  <p>The logic I'm trying to accomplish is this:</p>
  
  <pre><code>if (Model.Country.CountryName != null) {
      @Html.DropDownListFor(m =&gt; m.Country, new SelectList(Model.Countries, "Value", "Text"), Model.Country.CountryName)&lt;br /&gt;
  } else {
      @Html.DropDownListFor(m =&gt; m.Country, new SelectList(Model.Countries, "Value", "Text"), "Select a Country")&lt;br /&gt;
  }
  </code></pre>
  
  <p>If you remove the if else statement above (which doesn't work either), each <code>DropDownListFor</code> by itself works, except that the former throws a NullRefernceException when CountryName is null. The latter simply doesn't remember what the user chose in the past. Here is my controller:</p>
  
  <pre><code>        var userId = User.Identity.GetUserId();
          ApplicationUser user = await UserManager.FindByIdAsync(userId);
          var model = new IndexViewModel
          {
              HasPassword = HasPassword(),
              //PhoneNumber = await UserManager.GetPhoneNumberAsync(userId),
              //TwoFactor = await UserManager.GetTwoFactorEnabledAsync(userId),
              //Or we can write like this because of the user statement above.
              //PhoneNumber = user.PhoneNumber,
              //TwoFactor = user.TwoFactorEnabled,
              Logins = await UserManager.GetLoginsAsync(userId),
              BrowserRemembered = await AuthenticationManager.TwoFactorBrowserRememberedAsync(userId),
              Country = user.Country,
              State = user.State,
              City = user.City,
              Countries = new SelectList(db.Countries, "CountryId", "CountryName")
          };
          return View(model);
  </code></pre>
  
  <p>If I use something like this in my controller model:</p>
  
  <pre><code>Countries = new SelectList(db.Countries, "CountryId", "CountryName", user.Country.CountryName)
  </code></pre>
  
  <p>Then it throws a NullReferenceException at the controller. I understand why this is happening, but for the life of me can't find an answer after half a day's search.</p>
  
  <p>I'm just wondering if there is a clean way to handle the null value and therefore, pass a different default value if CountryId is null?</p>
  
  <p>Any help is always much appreciated.</p>
  
  <p><strong>Edit:</strong> I forgot to mention the viewmodel. I basically added all this to the IndexViewModel (although I'm not sure if I need it all at this point):</p>
  
  <pre><code>    public int CountryId { get; set; }
      public int StateId { get; set; }
      public int CityId { get; set; }
  
      public virtual Country Country { get; set; }
      public virtual State State { get; set; }
      public virtual City City { get; set; }
  
      public IEnumerable&lt;SelectListItem&gt; Countries { get; set; }
      public IEnumerable&lt;SelectListItem&gt; States { get; set; }
      public IEnumerable&lt;SelectListItem&gt; Cities { get; set; }
  </code></pre>