title: Retrieving data from Html.DropDownList() in controller (ASP MVC) | string returned?
body: <p>I have the following problem: </p>
  
  <p>I have a form in site/banen (currently local running webserver) which is using a SQL database. The link is made using ADO.net and is instantiated in the controller in the following way:</p>
  
  <pre><code>DBModelEntities _entities;
  _entities = new DBModelEntities(); // this part is in the constructor of the controller.
  </code></pre>
  
  <p>Next, I use this database to fill a Html.DropDownList() in my view. This is done in two steps. At the controller side we have in the constructor:</p>
  
  <pre><code>ViewData["EducationLevels"] = this.GetAllEducationLevels();
  </code></pre>
  
  <p>and a helper method:</p>
  
  <pre><code>public SelectList GetAllEducationLevels()
  {
       List&lt;EducationLevels&gt; lstEducationLevels = _entities.EducationLevels.ToList();
       SelectList slist = new SelectList(lstEducationLevels, "ID", "Name");
       return slist;
  }
  </code></pre>
  
  <p>In the view I have the following:</p>
  
  <pre><code>&lt;% using (Html.BeginForm()) {%&gt;
  
      &lt;fieldset&gt;
          &lt;legend&gt;Fields&lt;/legend&gt;
  
          &lt;!-- various textfields here --&gt;
  
          &lt;p&gt;
              &lt;label for="EducationLevels"&gt;EducationLevels:&lt;/label&gt;
              &lt;!-- &lt;%= Html.DropDownList("EducationLevels", ViewData["EducationLevels"] as SelectList)%&gt; --&gt;
              &lt;%= Html.DropDownList("EducationLevels", "..select option..")%&gt;
          &lt;/p&gt;
          &lt;p&gt;
              &lt;input type="submit" value="Create" /&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt;
  
  &lt;% } %&gt;
  </code></pre>
  
  <p>Now, the form is rendered correctly when I browse to the create page. I can select etc. But when selected I have to use that value to save in my new model to upload to the database. This is where it goes wrong. I have the following code to do this in my controller:</p>
  
  <pre><code>//
      // POST: /Banen/Create
  
      [AcceptVerbs(HttpVerbs.Post)]
      public ActionResult Create(FormCollection form)
      {
          // set rest of information which has to be set automatically
          var vacatureToAdd = new Vacatures();
          //vacatureToAdd.EducationLevels = form["EducationLevels"];
  
          // Deserialize (Include white list!)
          TryUpdateModel(vacatureToAdd);
  
          // Validate
          if (String.IsNullOrEmpty(vacatureToAdd.Title))
              ModelState.AddModelError("Title", "Title is required!");
          if (String.IsNullOrEmpty(vacatureToAdd.Content))
              ModelState.AddModelError("Content", "Content is required!");
  
          // Update the variables not set in the form
          vacatureToAdd.CreatedAt = DateTime.Now;                 // Just created.
          vacatureToAdd.UpdatedAt = DateTime.Now;                 // Just created, so also modified now.
          vacatureToAdd.ViewCount = 0;                            // We have just created it, so no views
          vacatureToAdd.ID = GetGuid();                           // Generate uniqueidentifier
  
          try
          {
              // TODO: Add insert logic here
              _entities.AddToVacatures(vacatureToAdd);
              _entities.SaveChanges();
  
              // Return to listing page if succesful
              return RedirectToAction("Index");
          }
          catch (Exception e)
          {
              return View();
          }
      } 
      #endregion
  </code></pre>
  
  <p>It gives the error:</p>
  
  <p><a href="http://www.bastijn.nl/zooi/error_dropdown.png" rel="nofollow noreferrer">alt text http://www.bastijn.nl/zooi/error_dropdown.png</a></p>
  
  <p>I have found various topics on this but all say you can retrieve by just using:</p>
  
  <pre><code>vacatureToAdd.EducationLevels = form["EducationLevels"];
  </code></pre>
  
  <p>Though this returns a string for me. Since I'm new to ASP.net I think I am forgetting to tell to select the object to return and not a string. Maybe this is the selectedValue in the part where I make my SelectList but I can't figure out how to set this correctly. Of course I can also be complete on a sidetrack.</p>
  
  <p>Sidenote: currently I'm thinking about having a seperate model like <a href="https://stackoverflow.com/questions/789297/asp-net-mvc-dropdownlist" title="here">here</a>.</p>
  
  <p>Any help is appreciated.</p>