title: asp.net mv 5, DropDownListFor with ViewModel not working
body: <p>I have an MVC 5 site, I would like to use a strongly typed DropDownListFor with a ViewModel - not with ViewBag.</p>
  
  <p>I have found various articles on this - but they all seem to have huge holes - for example this one doesnt cover editing, and I do not understand how or when "SelectedFlavourId" should be used.
  <a href="http://odetocode.com/blogs/scott/archive/2013/03/11/dropdownlistfor-with-asp-net-mvc.aspx" rel="nofollow">http://odetocode.com/blogs/scott/archive/2013/03/11/dropdownlistfor-with-asp-net-mvc.aspx</a></p>
  
  <p>I have several requirements.</p>
  
  <ul>
  <li>When editing the story I would like a drop down list of all places to
  be displayed - with the associated place (if any) - selected. </li>
  <li>I want to use the strongly typed DropDownListFOR (as opposed to 
  DropDownList).</li>
  <li>I would like to use a ViewModel not the ViewBag.</li>
  <li>I want to add a "No Associated Place" which will be
  selected if PlaceId is null.</li>
  <li>I want to add a css class = "form-control" to the DropDownListFor.</li>
  </ul>
  
  <p>The below is as far as I have got after a day of frustration.</p>
  
  <p>A story can be optionally associated with a PlaceId. A blank placeId is also valid. A place can also be associated with more than one story.</p>
  
  <p><strong>Models</strong></p>
  
  <pre><code>public class Place
  {
      public Guid Id { get; set; }
      public string PlaceName { get; set; }
  }   
  
  public class Story
  {
      public Guid Id { get; set; }
      public Guid? PlaceId { get; set; }
      public string StoryName { get; set; }
  }
  
  public class StoryPlaceDropdown
  {
      public Story story { get; set; }
      public Guid SelectedStoryId;
      public IEnumerable&lt;Place&gt; places;
      public IEnumerable&lt;SelectListItem&gt; placeItems
      {
          get
          {
              return new SelectList(places, "Id", "PlaceName");
          }
      }
  }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>public ActionResult Edit(Guid Id)
  {
      var spd = new StoryPlaceDropdown();
  
      spd.places = PlaceRepo.SelectAll();
      spd.story = StoryRepo.SelectStory(Id);
      spd.selectedStoryID = apd.story.Id;
  
      // Return view
      return View(spd);
  }
  
  [HttpPost]
  public ActionResult Edit(StoryPlaceDropdown spd)
  {
      // Never gets this far
      spd.Places = PlaceRepo.SelectAll();
  
      return View();
  }
  </code></pre>
  
  <p><strong>In View</strong></p>
  
  <pre><code>@Html.DropDownListFor(m =&gt; m.SelectedStoryId, Model.PlaceItems)
  </code></pre>
  
  <p>This populates the DropDownList fine. However it does not select the correct item in edit view. Also when I submit the form I get this error: 
  Object reference not set to an instance of an object. on this line in the view @Html.DropDownListFor(m => m.SelectedStoryId, Model.PlaceItems)</p>
  
  <p>How can I get this all working? Thanks.</p>