title: How to create a viewModel object for many-to-many relationships
body: <p>I'm new to ASP.NET and currently working on a simple app to show a list of people and their hobbies. I have the following classes:</p>
  
  <pre><code>public class Hobby
  {
      public int HobbyID                            {get;set;}
      public string Name                            {get;set;}
      public string Type                            {get;set;}
      public ICollection&lt;PersonHobby&gt; PersonHobbies {get;set;}
  }
  
  public class Person
  {
      public int PersonID                           {get;set;}
      public int Age                                {get;set;}
      public string Name                            {get;set;}
      public ICollection&lt;PersonHobby&gt; PersonHobbies {get;set;}
  }
  
  public class PersonHobby
  {
      public int PersonHobbyID                      {get;set;}
      public int PersonID                           {get;set;}
      public int HobbyID                            {get;set;}
  }
  </code></pre>
  
  <p>When viewing a person's Details page, I also need to display their hobbies. I did some research and found that ViewModels are a good way to accomplish this. So I created one, but I'm not sure if I did it correctly:</p>
  
  <pre><code>public class PersonHobbiesViewModel
  {
      public Person Person                          {get;set;}
      public IEnumerable&lt;PersonHobby&gt; PersonHobbies {get;set;}
      public IEnumerable&lt;Hobby&gt; Hobbies             {get;set;}
  }
  </code></pre>
  
  <p>And at this point I know that I need to create a viewmodel object in my controller's Details method and populate it with data, but I don't know how to navigate through the different tables. I have this:</p>
  
  <pre><code>public ActionResult Details(int? id)
  {
      var viewModel = new PersonHobbiesViewModel();
      viewModel.Person = db.Person.find(id);
      viewModel.Hobbies = ???
  
      return View(viewModel);
  }
  </code></pre>
  
  <p>On the other hand, if I'm going in the completely wrong direction, let me know! Thanks in advance.</p>