title: ViewModel with SelectList binding in ASP.NET MVC2
body: <p>I am trying to implement an Edit ViewModel for my Linq2SQL entity called Product. It has a foreign key linked to a list of brands. </p>
  
  <p>Currently I am populating the brand list via ViewData and using DropDownListFor, thus:</p>
  
  <pre><code>&lt;div class="editor-field"&gt;
      &lt;%= Html.DropDownListFor(model =&gt; model.BrandId, (SelectList)ViewData["Brands"])%&gt;
      &lt;%= Html.ValidationMessageFor(model =&gt; model.BrandId) %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Now I want to refactor the view to use a strongly typed ViewModel and Html.EditorForModel():</p>
  
  <pre><code>&lt;% using (Html.BeginForm()) {%&gt;
      &lt;%= Html.ValidationSummary(true) %&gt;
  
      &lt;fieldset&gt;
          &lt;legend&gt;Fields&lt;/legend&gt;
  
          &lt;%=Html.EditorForModel() %&gt;
  
          &lt;p&gt;
              &lt;input type="submit" value="Save" /&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt;
  
  &lt;% } %&gt;
  </code></pre>
  
  <p>In my Edit ViewModel, I have the following:</p>
  
  <pre><code>public class EditProductViewModel
  {
      [HiddenInput]
      public int ProductId { get; set; }
  
      [Required()]
      [StringLength(200)]
      public string Name { get; set; }
  
      [Required()]
      [DataType(DataType.Html)]
      public string Description { get; set; }
  
      public IEnumerable&lt;SelectListItem&gt; Brands { get; set; }
  
      public int BrandId { get; set; }
  
      public EditProductViewModel(Product product, IEnumerable&lt;SelectListItem&gt; brands)
      {
          this.ProductId = product.ProductId;
          this.Name = product.Name;
          this.Description = product.Description;
          this.Brands = brands;
          this.BrandId = product.BrandId;
      }
  }
  </code></pre>
  
  <p>The controller is setup like so:</p>
  
  <pre><code>public ActionResult Edit(int id)
  {
      BrandRepository br = new BrandRepository();
  
      Product p = _ProductRepository.Get(id);
      IEnumerable&lt;SelectListItem&gt; brands = br.GetAll().ToList().ToSelectListItems(p.BrandId);
  
      EditProductViewModel model = new EditProductViewModel(p, brands);
  
      return View("Edit", model);
  }
  </code></pre>
  
  <p>The ProductId, Name and Description display correctly in the generated view, but the select list does not. The brand list definitely contains data.</p>
  
  <p>If I do the following in my view, the SelectList is visible:</p>
  
  <pre><code>&lt;% using (Html.BeginForm()) {%&gt;
      &lt;%= Html.ValidationSummary(true) %&gt;
  
      &lt;fieldset&gt;
          &lt;legend&gt;Fields&lt;/legend&gt;
  
          &lt;%=Html.EditorForModel() %&gt;
  
          &lt;div class="editor-label"&gt;
              &lt;%= Html.LabelFor(model =&gt; model.BrandId) %&gt;
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              &lt;%= Html.DropDownListFor(model =&gt; model.BrandId, Model.Brands)%&gt;
              &lt;%= Html.ValidationMessageFor(model =&gt; model.BrandId) %&gt;
          &lt;/div&gt;
          &lt;p&gt;
              &lt;input type="submit" value="Save" /&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt;
  
  &lt;% } %&gt;
  </code></pre>
  
  <p>What am I doing wrong? Does EditorForModel() not generically support the SelectList? Am I missing some kind of DataAnnotation?</p>
  
  <p>I can't seem to find any examples of SelectList usage in ViewModels that help. I'm truly stumped. <a href="https://stackoverflow.com/questions/295313/modelbinding-with-selectlist/1221499#1221499">This answer</a> seems to be close, but hasn't helped.</p>