title: How do I put list of BrandNames from Database in a dropdownlist
body: <p>I am trying to put a list of medical product <em>brand names</em> in a <em>dropdown list</em> on my edit.cshtml file using <code>Html.DropDownListFor()</code>.  I am trying to use the Html helper properly but my efforts have been unsuccessful.  Model.BrandSelectListItem is an IEnumerable that is supposed to go into the DropDownList.  </p>
  
  <p>Also, I don't understand why I need to reference the model as 'model' in the lambda expression, but as 'Model' in the second argument of the following code segment:</p>
  
  <pre><code>@Html.DropDownListFor(model =&gt; model.BrandName, Model.BrandSelectListItem)
  </code></pre>
  
  <p>when executing the code, I receive the following run-time error:</p>
  
  <blockquote>
    <p>The ViewData item that has the key 'BrandName' is of type
    'System.String' but must be of type 'IEnumerable'.</p>
  </blockquote>
  
  <p>Here are some classes that may be relevant to this error:</p>
  
  <h2>MedicalProduct</h2>
  
  <pre><code>public class MedicalProduct
  {
      [Key]
      public int ID { get; set; }
  
      [Required]
      [StringLength(50)]
      public string Name { get; set; }
  
      [Required]
      [DataType(DataType.Currency)]
      public double Price { get; set; }
  
      // is a foreign key
      public Nullable&lt;int&gt; BrandID { get; set; }
  }
  </code></pre>
  
  <h2>MedicalProductViewModel</h2>
  
  <pre><code>public class MedicalProductViewModel
  {
      [Key]
      public int ID { get; set; }
  
      [Required]
      [StringLength(50)]
      public string Name { get; set; }
  
      [Required]
      [DataType(DataType.Currency)]
      public double Price { get; set; }
  
      public Nullable&lt;int&gt; BrandID { get; set; }
  
      [DisplayFormat(NullDisplayText="[Generic]")]
      public string BrandName { get; set; }
  
      public IEnumerable&lt;SelectListItem&gt; BrandSelectListItem { get; set; }
  }
  </code></pre>
  
  <h2>MedicalProductController</h2>
  
  <pre><code>public class MedicalProductController : Controller
  {
      private MvcMedicalStoreDb _db = new MvcMedicalStoreDb();
  
      //
      // GET: /MedicalSupply/Edit/5
  
      public ActionResult Edit(int id = 0)
      {
          MedicalProduct medicalProduct = _db.Products.Find(id);
          if (medicalProduct == null)
          {
              return HttpNotFound();
          }
  
          var viewModel = GetMedicalProductViewModel(medicalProduct);
          return View(viewModel);
      }
  
      public MedicalProductViewModel GetMedicalProductViewModel(MedicalProduct product)
      {
          var mapper = new MedicalProductMapper(_db);
  
          return mapper.GetMedicalProductViewModel(product);            
      }
  }
  </code></pre>
  
  <h2>MedicalProductMapper</h2>
  
  <pre><code>public class MedicalProductMapper
  {
      private MvcMedicalStoreDb _db;
  
      public MedicalProductMapper(MvcMedicalStoreDb db)
      {
          _db = db;
      }
  
      public MedicalProductViewModel GetMedicalProductViewModel(MedicalProduct source)
      {
          MedicalProductViewModel viewModel = new MedicalProductViewModel();
          var dbBrands = _db.Brands.ToArray();
  
          viewModel.ID = source.ID; 
          viewModel.Name = source.Name;
          viewModel.Price = source.Price;
          viewModel.BrandID = source.BrandID;
          if (viewModel.BrandID != null)
              viewModel.BrandName = dbBrands.SingleOrDefault(b =&gt; b.ID == source.BrandID).Name;
  
          var queryBrands = from b in dbBrands
                            select b;
  
          // BrandSelectListItem is 'null' before this assignment statement executes. 
          // and Stays null after the statement executes, why?
          viewModel.BrandSelectListItem = queryBrands as IEnumerable&lt;SelectListItem&gt;;
  
          return viewModel;
      }
  }
  </code></pre>
  
  <h2>Edit.cshtml</h2>
  
  <pre><code>@model MvcMedicalStore.Models.MedicalProductViewModel
  
  @{
      ViewBag.Title = "Edit";
  }
  
  &lt;h2&gt;Edit&lt;/h2&gt;
  
  @using (Html.BeginForm()) {
      @Html.AntiForgeryToken()
      @Html.ValidationSummary(true)
  
      &lt;fieldset&gt;
          &lt;legend&gt;MedicalProduct&lt;/legend&gt;
  
          @Html.HiddenFor(model =&gt; model.ID)
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.Name)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.Name)
              @Html.ValidationMessageFor(model =&gt; model.Name)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.Price)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.Price)
              @Html.ValidationMessageFor(model =&gt; model.Price)
          &lt;/div&gt;
  
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.BrandName)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.DropDownListFor(model =&gt; model.BrandName, Model.BrandSelectListItem)
              @Html.ValidationMessageFor(model =&gt; model.BrandName)
          &lt;/div&gt;
  
          &lt;p&gt;
              &lt;input type="submit" value="Save" /&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt;
  }
  
  &lt;div&gt;
      @Html.ActionLink("Back to List", "Index")
  &lt;/div&gt;
  
  @section Scripts {
      @Scripts.Render("~/bundles/jqueryval")
  }
  </code></pre>