title: Dropdownlist and ViewData in ASP.NET MVC 4
body: <p>I have a problem with my code. Iwan to make a dropdownlist in my View.cshtml using ViewData and @Html.Dropdownlist. But i get error while i compile it.
  Here is my code : </p>
  
  <p>This is the model </p>
  
  <pre><code>public class DeviceComponentModel
  {
      public int devcomId { get; set; }
      public int devcomcomponentId { get; set; }
      public int? devcomdeviceId { get; set; }
      public ComponentModel Component { get; set; }
      public DeviceModel Device { get; set; }
  }
  </code></pre>
  
  <p>This is the code in the controller.</p>
  
  <pre><code>public ActionResult addDevicecomponent()
  {
      ViewData["Device"] = new SelectList(debuDB.Devices, "Id", "Name");
      ViewData["Component"] = new SelectList(debuDB.Components, "Id", "Name");
      DeviceComponentModel mdlDevicecom = new DeviceComponentModel();
      return View(mdlDevicecom);
  }
  
  [HttpPost]
  public ActionResult addDeviceComponent(DeviceComponentModel mdlDevicecom)
  {
      try
      {
          DeviceComponent deviceComponent = new DeviceComponent()
          {
              Id = mdlDevicecom.devcomId,
              device_id = mdlDevicecom.devcomdeviceId,
              component_id = mdlDevicecom.devcomcomponentId
          };
          debuDB.DeviceComponents.InsertOnSubmit(deviceComponent);
          debuDB.SubmitChanges();
          return RedirectToAction("Index");
      }
      catch
      {
          return View(mdlDevicecom);
      }
  }
  </code></pre>
  
  <p>This is my code in the View</p>
  
  <pre><code>&lt;div class="editor-label"&gt;
      @Html.LabelFor(model =&gt; model.devcomdeviceId)
  &lt;/div&gt;
  &lt;div class="editor-field"&gt;
      @Html.DropDownList("device_0",ViewData["Device"] as IEnumerable&lt;SelectListItem&gt;, "Choose Device")
  &lt;/div&gt;
  
  &lt;div class="editor-label"&gt;
      @Html.LabelFor(model =&gt; model.devcomcomponentId)
  &lt;/div&gt;
  &lt;div class="editor-field"&gt;
      @Html.DropDownList("component_0",ViewData["Component"] as IEnumerable&lt;SelectListItem&gt;, "Choose Component")
  &lt;/div&gt;
  </code></pre>
  
  <p>But when i compiled it i got the error like this:</p>
  
  <blockquote>
    <p>"There is no ViewData item of type 'IEnumerable' that has the key 'device_0'."</p>
  </blockquote>
  
  <p>I dont know why cant i get the error like that. I hope some of you can give the solution of this error.</p>
  
  <p>Regards,</p>