title: MVC-3 DropdownList or DropdownListFor - Can't save values in controller POST
body: <p>I searches for hours (or days) and didn't find a solution yet. I want to edit a customer with a DropdownListFor for the salutation with the right preselected value.</p>
  
  <p>I've got 3 entities (Database first concept, this is not my own design...): customer, address, salutation</p>
  
  <p>A CUSTOMER has an address_id (f_key) and an ADDRESS has got a salutation_id (f_key). The ADDRESS holds the first and last name for example. Inside the SALUTATION entity there is a column sal1 which holds all possible salutations.</p>
  
  <p>Now, I want to edit my customer via a ViewModel which looks like this:</p>
  
  <pre><code>public class CustomerViewModel
  {
      public CUSTOMER cust { get; set; }
      public SelectList salutationList { get; set; }
  
      CustomerRepository repository = new CustomerRepository();
  
      public CustomerViewModel(int id)
      {
          cust = repository.GetCustomerByIdAsQueryable(id).Single();
          salutationList = new SelectList(repository.GetSalutations(), cust.ADDRESS.SALUTATION.SAL1);
      }
      // Some more
  }
  </code></pre>
  
  <p>The CutsomerRepository methods:</p>
  
  <pre><code>public class CustomerRepository
      {
          private MyEntities db = new MyEntities();
  
          public IQueryable&lt;CUSTOMER&gt; GetCustomerByIdAsQueryable(int id) {...}
  
          public IQueryable&lt;CUSTOMER&gt; GetCustomersByName(string firstName, string lastName, int maxCount)  {...}
  
          public List&lt;string&gt; GetSalutations()
          {
              var salutationList = new List&lt;string&gt;();
              var salutationListQry = from s in db.SALUTATION
                                      select s.SAL1;
              salutationListTemp.AddRange(salutationListQry);
              return salutationList;
          }
          // ...
      }
  </code></pre>
  
  <p>This is my controller method:</p>
  
  <pre><code>[HttpPost]
  public ActionResult CustomerData(int id, FormCollection fc)
  {
      var vm = new CustomerViewModel(id);
      // Why do I need the following line?
      vm.cust = repository.GetCustomerByIdAsQueryable(id).Single();
      try
      {
          UpdateModel(vm, fc);
          repository.Save();
          return View("CustomerData", vm);
      }
      catch (Exception e)
      {
          return View();
      }
  }
  </code></pre>
  
  <p>And finally the part from my View:</p>
  
  <pre><code>@model WebCRM.ViewModels.CustomerViewModel
  @using (Html.BeginForm())
  {
      // ...
      &lt;div class="editor-label"&gt;
          @Html.Label("Salutation:")
          @Html.DropDownListFor(model =&gt; model.cust.ADDRESS.SALUTATION.SAL1, Model.salutationList)
          // @Html.DropDownList("Salutation", Model.salutationList)
      &lt;/div&gt;
      &lt;div class="editor-label"&gt;
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.Label("Last name:")
          @Html.EditorFor(model =&gt; model.cust.ADDRESS.LASTNAME)
          @Html.ValidationMessageFor(model =&gt; model.cust.ADDRESS.LASTNAME)
      &lt;/div&gt;
      &lt;p&gt;
          &lt;input type="submit" value="Speichern" /&gt;
      &lt;/p&gt;
  }
  </code></pre>
  
  <p>Changing and saving last names works fine. But when saving the salutation it changes the SAL1 value in the SALUTATION entity to the one I've chosen in the DropdownListFor. What I want is to change the salutation_id inside the ADDRESS entity for my customer. Why isn't that working?</p>
  
  <p>Another strange behavoior: When removing the marked line in my CustomerController, I can't even change and save the last name. Normally the constructor of the CustimerViewModel sets the customer. So why do I have to have the line of code for setting the customer inside my ViewModel? It's duplicated, but has to be there...</p>
  
  <p>Thanks in advance.</p>