title: DropDownListFor Always Null on Postback
body: <p>I'm pulling my hair out trying to get dropdown lists to work in my Razor Views, I cannot find a solution that works and I can only assume I am missing something obvious..</p>
  <p>I am trying to create a sign-up page with a form for personal details for students which uses textboxes (working fine) and dropdowns to select items from lookup tables and insert the key as a foreign key in my new record. So, my student has a title - I populate the dropdown with all of the Salutations &amp; IDs from my DB, allow the user to select one and then save this with my new student record on posting back to the controller. The dropdowns populate fine, the values are all there, the user can select a value but it is always null on posting back??</p>
  <p>Example given below relates to just one such field in my Model, there are lots. In trying to get it to work I am using all textboxes and just one dropdown so the problem is below somewhere, not hidden in other code.</p>
  <p>My model (ViewCandidate) has (along with other fields):</p>
  <pre><code>public int SalutationID {get; set;}//foreign key to be saved when I pass the values from my View object to a domain entity object
  public Dictionary&lt;int, string&gt; Salutations {get; set;} //key/value pairs to fill the dropdown in the view
  </code></pre>
  <p>When I create the Model in the controller I populate the dropdowns like so:</p>
  <pre><code>public ViewResult Link()
  {    
      return View(new ViewCandidate{ Salutations = GetSalutations(entities) }); //'entities' is my Entity Framework context, all the entities in my data model/database
  }
  
  public static Dictionary&lt;int, string&gt; GetSalutations(DBEntities entities)
  {
      Dictionary&lt;int, string&gt; dictionary = new Dictionary&lt;int, string&gt;();
      foreach(var s in entities.Salutations)
      {
          dictionary.Add(s.SalutationID, s.Salutation);
      }
      return dictionary;
  }
  </code></pre>
  <p>And in my Razor View I am using Model Binding like so:</p>
  <pre><code>@model ViewModels.ViewCandidate
  ...
  
  &lt;div class=&quot;align-center&quot;&gt;
          &lt;label&gt;Title&lt;/label&gt;
          @Html.DropDownListFor(x =&gt; x.SalutationID, new SelectList(Model.Salutations, &quot;Key&quot;, &quot;Value&quot;), &quot;Please select your title&quot;, new { @class=&quot;form-control&quot;})
      &lt;/div&gt;
  </code></pre>
  <p>When I run the application and navigate to the Link Controller page this is the html generated for the dropdown:</p>
  <pre><code>&lt;div class=&quot;align-center&quot;&gt;
  &lt;label&gt;Title&lt;/label&gt;
  &lt;select class=&quot;form-control&quot; data-val=&quot;true&quot; data-val-number=&quot;The field SalutationID must be a number.&quot; data-val-required=&quot;The SalutationID field is required.&quot; id=&quot;SalutationID&quot; name=&quot;SalutationID&quot;&gt;&lt;option value=&quot;&quot;&gt;Please select your title&lt;/option&gt;
  &lt;option value=&quot;1&quot;&gt;Mr&lt;/option&gt;
  &lt;option value=&quot;2&quot;&gt;Mrs&lt;/option&gt;
  &lt;option value=&quot;3&quot;&gt;Miss&lt;/option&gt;
  &lt;option value=&quot;4&quot;&gt;Ms&lt;/option&gt;
  &lt;option value=&quot;5&quot;&gt;Master&lt;/option&gt;
  &lt;option value=&quot;6&quot;&gt;Doctor&lt;/option&gt;
  &lt;/select&gt;
  &lt;/div&gt;
  </code></pre>
  <p>The dropdown is generated just fine with the proper values. But when I complete the rest of the form and submit to post it back I am taken back to here in Visual Studio (the line from my Razor View):</p>
  <pre><code> @Html.DropDownListFor(x =&gt; x.SalutationID, new SelectList(Model.Salutations, &quot;Key&quot;, &quot;Value&quot;), &quot;Please select your title&quot;, new { @class=&quot;form-control&quot;})
  </code></pre>
  <p>With the following error:</p>
  <blockquote>
  <p>An exception of type 'System.ArgumentNullException' occurred in System.Web.Mvc.dll but was not handled in user code</p>
  <p>Value cannot be null.</p>
  <p>Parameter name: items</p>
  </blockquote>
  <p>'Items' is the parameter name of the IEnumerable in the SelectList constructor, which has items and has rendered them all fully. None of the salutation records in my DB or rendered in the view are NULL. I cannot get this to work and it is driving me mad, can anyone help??</p>
  <p>If it makes any difference I am using Entity Framework - so the entity I eventually save is 'Candidate' to which I pass the values from my ViewCandidate created in the view. The Saluations I iterate over to create the <code>Dictionary&lt;int, string&gt;</code> are EF objects. Wrapping these in View Objects didn't help either.</p>