title: Best way to populate SelectList for ViewModel on GET/POST
body: <p>I have the following ViewModel:</p>
  
  <pre><code>public class EditViewModel
  {
      public int FooType { get; set; }
      public IEnumerable&lt;SelectListItem&gt; FooTypes { get; set; }
  }
  </code></pre>
  
  <p>I originally populated it in my Edit action like so:</p>
  
  <pre><code>public ActionResult Edit(int id)
  {
      EditViewModel model = new EditViewModel();
      model.FooTypes = new SelectList(repository.GetFooTypes(), "Id", "Value");
  
      return View(model);
  }
  </code></pre>
  
  <p>When I created the action to POST the values I had to repeat the same code:</p>
  
  <pre><code>public ActionResult Edit(int id, EditViewModel model)
  {
      if( !ModelState.IsValid )
      {
          model.FooTypes = new SelectList(repository.GetFooTypes(), "Id", "Value");
  
          return View(model);
      }
  
      return RedirectToAction("Index");
  }
  </code></pre>
  
  <p>I don't like having this code in two separate locations. Is there any common practice for refactoring this into a single spot so I dont need to repeat this code?</p>