title: Drop down troubles in .net using VB and view model
body: <p>OK I am new to .net, I am just taking a shot in the dark through this whole thing. I have an Addresses table with a StateID and CountryID field. They refer to the States and Countries table. I am using LINQ to SQL and Visual Studio 2010. </p>
  
  <p>In my AddressesController I have:</p>
  
  <pre><code>    Function Create() As ActionResult
          Dim these_states = GetStates()
          Dim these_countries = GetCountries()
          Dim viewModel = New AddressViewModel(these_states, these_countries)
          Return View(viewModel)
      End Function
  
      '
      ' POST: /Addresses/Create
  
      &lt;HttpPost()&gt; _
      Function Create(ByVal this_address As Address) As ActionResult
          dataContext.Addresses.InsertOnSubmit(this_address)
          dataContext.SubmitChanges()
          Return RedirectToAction("Index")
      End Function
  </code></pre>
  
  <p>I created an AddressViewModel and it looks like this:</p>
  
  <pre><code>Public Class AddressViewModel
  Public Address As Address
  Private _these_states As System.Linq.IQueryable(Of State)
  Private _these_countries As System.Linq.IQueryable(Of Country)
  
  Sub New(ByVal these_states As System.Linq.IQueryable(Of State), ByVal these_countries As System.Linq.IQueryable(Of Country))
      _these_states = these_states
      _these_countries = these_countries
  End Sub
  
  
  Dim StateList As New SelectList(_these_states, "StateID", "Label", 1)
  Public Property States As SelectList
      Get
          Return StateList
      End Get
      Set(ByVal value As SelectList)
  
      End Set
  End Property
  
  Dim CountryList As New SelectList(_these_countries, "CountryID", "Label", 1)
  Public Property Countries As SelectList
      Get
          Return CountryList
      End Get
      Set(ByVal value As SelectList)
  enter code here
  </code></pre>
  
  <p>Then in my view code I have:</p>
  
  <pre><code>    &lt;%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of TotallyAwesomeCRM.AddressViewModel)" %&gt;
  </code></pre>
  
  <p>some code removed as unnecessary</p>
  
  <pre><code>            &lt;div class="editor-label"&gt;
                  State
              &lt;/div&gt;
              &lt;div class="editor-field"&gt;
                  &lt;%: Html.DropDownList("StateID", Model.States, "1")%&gt;
                  &lt;%: Html.ValidationMessageFor(Function(model) model.StateID) %&gt;
              &lt;/div&gt;
  
              &lt;div class="editor-label"&gt;
                  Country
              &lt;/div&gt;
              &lt;div class="editor-field"&gt;
                  &lt;%: Html.DropDownList("CountryID", Model.Countries, "1")%&gt;
                  &lt;%: Html.ValidationMessageFor(Function(model) model.CountryID) %&gt;
              &lt;/div&gt;
  </code></pre>
  
  <p>So when I try to go to Addresses/Create I get this error:</p>
  
  <pre><code>ArgumentNullException was not handled by user code:Value cannot be null.
  </code></pre>
  
  <p>Parameter name: items</p>
  
  <p>And it points to this line in the AddressViewModel:</p>
  
  <pre><code>Dim StateList As New SelectList(_these_states, "StateID", "Label", 1)
  </code></pre>
  
  <p>I know it pulls results, so I looked up the item Parameter and msdn says it is System.Collections.IEnumerable</p>
  
  <p>So I changed the AddessViewModel to:</p>
  
  <pre><code>Public Address As Address
  Private _these_states As System.Collections.IEnumerable
  Private _these_countries As System.Collections.IEnumerable
  
  Sub New(ByVal these_states As System.Collections.IEnumerable, ByVal these_countries As System.Collections.IEnumerable)
      _these_states = these_states
      _these_countries = these_countries
  End Sub
  </code></pre>
  
  <p>Got the same error, please help</p>