title: How to pass an entire ViewModel back to the controller
body: <p>I have a ViewModel that contains two objects: </p>
  
  <pre><code>public class LookUpViewModel
  {
      public Searchable Searchable { get; set; }
      public AddToSearchable AddToSearchable { get; set; }
  }
  </code></pre>
  
  <p>The two contained models look something like this:</p>
  
  <pre><code>public class Searchable
  {
      [Key]
      public int SearchableId { get; set; }
      public virtual ICollection&lt;AddToSearchable&gt; AddedData { get; set; }
  }
  
  public class AddToSearchable
  {
      [Key]
      public int AddToSearchableId { get; set;}
      [Required]
      public int SearchableId { get; set; }
      [Required]
      public String Data { get; set; }
      [Required]
      public virtual Searchable Searchable { get; set; }
  }
  </code></pre>
  
  <p>I have a view that uses my <code>LookUpViewModel</code> and receives input to search for a <code>SearchableId</code>. If the <code>Searchable</code> object is found, a <code>LookUpViewModel</code> object is created and passed to the View. The view then displays editor fields for <code>AddToSearchable.Data</code>. Once submitted, I want the <code>LookUpViewModel</code> to be passed to an action method to handle all the back-end code. The only problem is, the <code>LookUpViewModel</code> passed to my action method contains a null reference to <code>Searchable</code> and a valid reference to <code>AddToSearchable</code>.. i.e. I'm missing half of my data.</p>
  
  <p>Here's an example of what my view looks like:</p>
  
  <pre><code>@model HearingAidTrackingSystem.ViewModels.LookUpViewModel
  
  @using (Html.BeginForm("LookUp", "Controller", "idStr", FormMethod.Post))
  {
      &lt;input type="text" name="idStr" id="idStr"/&gt;
      &lt;input type="submit" value="Search" /&gt;
  }
  
  @if (Model.Searchable != null &amp;&amp; Model.AddToSearchable != null)
  {
      using (Html.BeginForm("AddMyStuff", "Controller"))
      {
          Html.HiddenFor(model =&gt; model.Searchable.SearchableId);
          Html.HiddenFor(model =&gt; model.Searchable.AddedData);
          Html.HiddenFor(model =&gt; model.AddToSearchable.AddToSearchableId);
          Html.HiddenFor(model =&gt; model.AddToSearchable.SearchableId);
          Html.HiddenFor(model =&gt; model.AddToSearchable.Searchable);
  
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.AddToSearchable.Data)
              @Html.ValidationMessageFor(model =&gt; model.AddToSearchable.Data);
          &lt;/div&gt;                                    
  
          &lt;input type="submit" value="Submit" /&gt;
      }
  }
  </code></pre>
  
  <p>and here are my action methods:</p>
  
  <pre><code>public ActionResult LookUp(LookUpViewModel vm)
  {
      return View(vm);
  }
  
  [HttpPost]
  public ActionResult LookUp(string idStr)
  {
      int id = /*code to parse string to int goes here*/;
  
      Searchable searchable = dal.GetById(id);
      LookUpViewModel vm = new LookUpViewModel { Searchable = searchable, 
                                                 AddToSearchable = new AddToSearchable() };
      //When breakpoint is set, vm contains valid references
      return View(vm);
  }
  
  [HttpPost]
  public ActionResult AddMyStuff(LookUpViewModel vm)
  {
      //**Problem lies here**
  
      //Do backend stuff
  }       
  </code></pre>
  
  <p>Sorry for the lengthy post. I tried my best to keep it simple. Any suggestions you may have.. fire away.</p>