title: MVC 3: DropDownList on an Edit Form for an object that is a property of a ViewModel
body: <p><strong>Overview:</strong>
  I'm trying to use a ViewModel that has a property that is the model class that I'm trying to edit. I've seen the edit form work using the MVC scaffolding edit forms when editing a model directly, however I am trying to use a ViewModel that contains the model being edited. Everything works except for the saving of a field that's displayed in a DropDownList.</p>
  
  <p><strong>Explanation:</strong>
  I've attempted to use the scaffolding features of MVC 3 to create an edit form for a model.
  In the MVC Music Store tutorial, this is done for the Edit page of an <code>Album</code>, in the <code>StoreManagerController</code>.
  Within that page, they have two drop downs for Genre and Artist. Each looks similar to this in the view:-</p>
  
  <pre><code>&lt;div class="editor-label"&gt;
      @Html.LabelFor(model =&gt; model.GenreId, "Genre")
  &lt;/div&gt;
  &lt;div class="editor-field"&gt;
      @Html.DropDownList("GenreId", String.Empty)
      @Html.ValidationMessageFor(model =&gt; model.GenreId)
  &lt;/div&gt;
  </code></pre>
  
  <p>As far as I can tell, these have their options filled out in the controller using the ViewBag.</p>
  
  <pre><code>ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
  ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
  </code></pre>
  
  <p><strong>Working with the Model directly</strong></p>
  
  <p>In my code, I managed to do the same with an object that's being saved to the DB through Entity Framework.</p>
  
  <p>Model</p>
  
  <pre><code>public class Season
  {
      public int SeasonId { get; set; }
      public int ClubId { get; set; }
      public Club Club { get; set; }
  }
  </code></pre>
  
  <p>Code in Controller</p>
  
  <pre><code>ViewBag.ClubId = new SelectList(clubs, "ClubId", "Name", season.ClubId);
  </code></pre>
  
  <p>View</p>
  
  <pre><code>&lt;div class="editor-label"&gt;
      @Html.LabelFor(model =&gt; model.ClubId, "Club")
  &lt;/div&gt;
  &lt;div class="editor-field"&gt;
      @Html.DropDownList("ClubId", String.Empty)
      @Html.ValidationMessageFor(model =&gt; model.ClubId)
  &lt;/div&gt;
  </code></pre>
  
  <p>This works fine.</p>
  
  <p><strong>Working with the View Model</strong></p>
  
  <p>However now I have realised that the page needs more text displayed than what is available in the model I'm editing.
  I was hoping to create a special View Model, with the edited model (season) and the extra text I want to display.</p>
  
  <p>ViewModel</p>
  
  <pre><code>public class EditSeasonViewModel
  {
      public string PlayerName {get; set;}
      public string SummaryText {get; set;}
      public int PlayerId {get; set;}
      public Season season {get; set;}
  }
  </code></pre>
  
  <p>I did this, changed the controller to have the HttpGet and HttpPost methods use the new ViewModel, changed the View to accept the new ViewModel, and changed the all 'EditorFor' methods in the view to use model.season.MyProperty.</p>
  
  <p>Code in Controller</p>
  
  <pre><code>ViewBag.ClubId = new SelectList(clubs, "ClubId", "Name", seasonVM.season.ClubId);
  </code></pre>
  
  <p>Code in View    </p>
  
  <pre><code>&lt;div class="editor-label"&gt;
      @Html.LabelFor(model =&gt; model.season.ClubId, "Club")
  &lt;/div&gt;
  &lt;div class="editor-field"&gt;
      @Html.DropDownList("ClubId", String.Empty)
      @Html.ValidationMessageFor(model =&gt; model.season.ClubId)
  &lt;/div&gt;
  </code></pre>
  
  <p>When debugging the HttpPost method, all of the values for season properly exist, except for the ClubId value which should come from the DropDropList.</p>
  
  <p>I haven't changed the DropDownList in the View at all from the way it was when we were using the Model directly.</p>
  
  <p><strong>Question:</strong>
  My question is, what do I need to change to get the ClubId to be saved properly when using this new <code>EditSeasonViewModel</code>?</p>
  
  <p>Also, how does the ViewBag.ClubId in the HttpGet method in the controller match to the DropDownList in the View, and then have it's value passed back to the HttpPost method?</p>