title: Cascading dropdown lists MVC3
body: <p>Relatively new to MVC and trying to get a cascading dropdown list working for train times.  </p>
  
  <p>After looking at a lot of posts, people say that you should stay away from ViewBag/ViewData and instead focus on ViewModels, but I just can't seem to get my head round it, and it's driving me up the wall.  Any tutorial seems to be either to complex or too easy and the whole viewModel idea just hasn't clicked with me yet.</p>
  
  <p>So here is my scenario:  I have an admin system where staff can add individual train journeys.  For each train time, I have an input form where the user can choose a Company, and from there, I'd like the dropdownlist underneath to populate with a list of journey numbers, which indicate routes. Once they have chosen a number, they can carry on with the rest of the form, which is quite large, including time of travel, facilities on the train etc.</p>
  
  <p>I've created a viewmodel like so:</p>
  
  <pre><code>public class JourneyNumbersViewModel
      {
          private List&lt;SelectListItem&gt; _operators = new List&lt;SelectListItem&gt;();
          private List&lt;SelectListItem&gt; _journeys= new List&lt;SelectListItem&gt;();
  
          [Required(ErrorMessage = "Please select an operator")]
          public string SelectedOperator { get; set; }
          [Required(ErrorMessage = "Please select a journey")]
          public string SelectedJourney { get; set; }
  
          public List&lt;SelectListItem&gt; Journeys
          {
              get { return _journeys; }
          }
          public List&lt;SelectListItem&gt; Operators
          {
              get
              {
                  foreach(Operator a in Planner.Repository.OperatorRepository.GetOperatorList())
                  {
                      _operators.Add(new SelectListItem() { Text = a.OperatorName, Value = a.OperatorID.ToString() });
                  }
                  return _operators;
              }
          }
      }
  </code></pre>
  
  <p>In my controller, I have this for the Create view:</p>
  
  <pre><code>    public ActionResult Create()
      {
          return View(new JourneyNumbersViewModel());
      }
  </code></pre>
  
  <p>And this is where it isn't really working for me - if I change my model at the top of the Create view to: <code>@model Planner.ViewModels.JourneyNumbersViewModel</code>, then the rest of my form throws errors as the model is no longer correct for the rest of the form.  Is this the way it is supposed to work - what if you need to reference multiple view models with a single view?</p>
  
  <p>I know this is a simple thing and as soon as it clicks I'll wonder how on earth I could have struggled with it in the first place, but if anyone can point out where I'm going wrong, I'd be very grateful.</p>