title: MVC Select List with Model at postback, how?
body: <p>I'm trying to create a view that will need 2 dropdown lists with MVC 3. In my only other MVC app we used the Telerik controls that used the Ajax method to populate data. Now on this project we don't use third party controls so I will be using the MVC <code>SelectList</code> for dropdowns. I've been reading a lot of articles on how to populate a <code>SelectList</code> but none of them say the same thing twice, always a different way to create the model, some use <code>ViewData</code> or <code>ViewBag</code> to hold the collections and pass to the view, etc.. no consistency.</p>
  
  <p>What is the best accepted method to populate a dropdown in an MVC view that uses the model itself for the data, not <code>ViewData</code>. And when the user makes a selection from the list, submits and the <code>HttpPost</code> action is called, how do I access the selected value from the Model property of the select list property?</p>
  
  <p>This is my current Model:</p>
  
  <pre><code>public class TemporaryRegistration {
      [Required]
      [Email(ErrorMessage = "Please enter a valid email address.")]
      [Display(Name = "Email address")]
      public string Email { get; set; }
  
      [Required]
      [Integer]
      [Min(1, ErrorMessage = "Please select an entity type.")]
      [Display(Name = "Entity Type")]
      public IEnumerable&lt;SelectListItem&gt; EntityType { get; set; }
  
      [Required]
      [Integer]
      [Min(1, ErrorMessage = "Please select an associated entity.")]
      [Display(Name = "Associated Entity")]
      public IEnumerable&lt;SelectListItem&gt; AssociatedEntity { get; set; }
  }
  </code></pre>
  
  <p>This is my current view, it's only using <code>TextBoxFor</code> where I need to use the dropdowns, how do I turn them into dropdowns?</p>
  
  <pre><code>@model Web.Models.TemporaryRegistration
  
  &lt;script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"&gt;&lt;/script&gt;
  
  @using (Html.BeginForm()) {
      @Html.ValidationSummary(true)
      &lt;fieldset&gt;
      &lt;legend&gt;Create New ELM Select User&lt;/legend&gt;
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.Email)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.Email)
              @Html.ValidationMessageFor(model =&gt; model.Email)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.EntityType)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.EntityType)
              @Html.ValidationMessageFor(model =&gt; model.EntityType)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.AssociatedEntity)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.AssociatedEntity)
              @Html.ValidationMessageFor(model =&gt; model.AssociatedEntity)
          &lt;/div&gt;
  
          &lt;p&gt;
              &lt;input type="submit" value="Create" /&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt;
  }
  
  &lt;div&gt;
      @Html.ActionLink("Back to List", "Index")
  &lt;/div&gt;
  </code></pre>
  
  <p>This is my current Post action:
  How do I get the selected values out?</p>
  
  <pre><code>[HttpPost]
  public ActionResult CreateUser(TemporaryRegistration registrationModel) {
      string newRegistrationGUID = string.Empty;
      if (!ModelState.IsValid) {
          return View();
      }
  
      TemporaryRegistrationEntity temporaryRegistration = null;
      temporaryRegistration = new TemporaryRegistrationEntity(registrationModel.Email, registrationModel.EntityType, registrationModel.AssociatedEntity);
      newRegistrationGUID = temporaryRegistration.Save();
      return Content("New registration was created with GUID " + newRegistrationGUID);
  }
  </code></pre>