title: DropDownListFor and relating my lambda to my ViewModel
body: <p>After googling for a while I'm still drawing a blank here. I'm trying to use a ViewModel to pull and provide a dictionary to a drop down list inside a strongly typed View:</p>
  <pre><code>&lt;%@ Page Title=&quot;&quot; Language=&quot;C#&quot; MasterPageFile=&quot;~/Views/Shared/Site.Master&quot; Inherits=&quot;EveNotebook.ViewModels.CorporationJoinViewModel&quot; %&gt;
  </code></pre>
  <p>...</p>
  <pre><code>&lt;%: Html.DropDownListFor(c =&gt; c.CorpDictionary.Keys, new SelectList(Model.CorpDictionary, &quot;Value&quot;, &quot;Key&quot;))%&gt;
  </code></pre>
  <p>I'm getting the error:</p>
  <blockquote>
  <p>CS1061: 'object' does not contain a definition for 'CorpDictionary' and no extension method 'CorpDictionary' accepting a first argument of type 'object' could be found</p>
  </blockquote>
  <p>and the relevant bit of my ViewModel</p>
  <pre><code>   public class CorporationJoinViewModel
      {
          DB _eveNotebook = new eveNotebookDB(); // data context
  
          public Dictionary&lt;int, string&gt; CorpDictionary
          {
              get
              {
                  Dictionary&lt;int, string&gt; corporations = new Dictionary&lt;int, string&gt;();
  
                  int x = 0;
                  foreach (Corporation corp in _db.Corporations)
                  {
                      corporations.Add(x, corp.name);
                  }
  
                  return corporations;
              }
          }
  </code></pre>
  <p>I'll admit I have a pretty magical understanding of how linq is finding my ViewModel object from that lambda, and the error message is making me think it's not. Is my problem the method I'm using to pass the data? What am I missing here?</p>
  <hr />
  <h2>Solution</h2>
  <p>(Very similar to the excellent answer but ran through the compiler and fixed a few typos in the process):</p>
  <p>Controller</p>
  <pre><code>  var model = new CorporationJoinViewModel
                  {
                      Corps = _eveNotebook.Corporations.Select( c =&gt; new SelectListItem
                                       {
                                            Text = c.name,
                                            Value = c.id.ToString()
                                       })
                  };
      
      return View(model);
  </code></pre>
  <p>View</p>
  <pre><code>Inherits=&quot;System.Web.Mvc.ViewPage&lt;IEnumerable&lt;EveNotebookLibrary.Models.Corporation&gt;&gt;&quot; %&gt;
  </code></pre>
  <p>...</p>
  <pre><code>&lt;%: Html.DropDownListFor(c =&gt; c.Corps, new SelectList(Model.Corps))%&gt;
  </code></pre>
  <p>ViewModel</p>
  <pre><code>public class CorporationJoinViewModel : ViewPage
  {
      public int CorporationId { get; set; }
  
      public IEnumerable&lt;SelectListItem&gt; Corps { get; set; }
  }
  </code></pre>