title: MVC Pass display data between views
body: <p>I have a view model that is used to display a form on one view, and then is also used to represent the POST data to an action. The action then displays another view model that contains much of the same data from the first view model. However, the first view model has several "display only" properties that are also required on the second view model (for display only on the second view also).</p>
  
  <p>I am wondering what the best way to pass this "display only" data to the second view would be. Currently, the best solution I have come up with is to have a bunch of hidden form fields that contain the display only property values, and then the model gets auto-populated for the action that handles the form POST. However, using hidden form fields seems very "hackish", and there seems like there should be a better solution to passing this data to another view The action doesn't need the display only information, it is only accessing it to populate the properties of the second view model that is passed to the second view.</p>
  
  <p>Let me just explain my question with code, as what I am after is probably better understood through code than words.</p>
  
  <p>Models:</p>
  
  <pre><code>public class SearchFilters
  {
      // ...
  }
  
  public class SearchResult
  {
      public int Id { get; set; }
      public bool Selected { get; set; }
      public string SomeDisplayValue1 { get; set; }
      public string SomeDisplayValue2 { get; set; }
      // ...
  }
  
  public class ResultsViewModel
  {
      public IList&lt;SearchResult&gt; Results { get; set; }
      // ...
  }
  
  public class DoSomethingWithSelectedResultsViewModel
  {
      public IList&lt;SearchResult&gt; SelectedResults { get; set; }
      public string SomeOtherProperty { get; set; }
      // ...
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>[HttpPost]
  public ActionResult Results(SearchFilters filters)
  {
      ResultsViewModel results = new ResultsViewModel();
      // ...
      return new View(results);
  }
  
  [HttpPost]
  public ActionResult DoSomethingWithSelectedResults(ResultsViewModel model)
  {
      // ...
      return View(new DoSomethingWithSelectedResultsViewModel
      {
          SelectedResults = model.Results.Where(r =&gt; r.Selected).ToList(),
          SomeOtherProperty = "...",
          // ...
      });
  }
  </code></pre>
  
  <p>View: Results.cshtml</p>
  
  <pre><code>@model ResultsViewModel
  @using (Html.BeginForm("DoSomethingWithSelectedResults", "Search"))
  {
      &lt;table&gt;
      for (int i = 0; i &lt; Model.Results.Count; i++)
      {
          &lt;tr&gt;
              &lt;td&gt;
                  @Html.CheckBoxFor(m =&gt; Model.Results[i].Selected)
  
                  @* I would like to eliminate these hidden inputs *@
                  @Html.HiddenFor(m =&gt; Model.Results[i].Id)
                  @Html.HiddenFor(m =&gt; Model.Results[i].SomeDisplayValue1)
                  @Html.HiddenFor(m =&gt; Model.Results[i].SomeDisplayValue2)
              &lt;/td&gt;
              &lt;td&gt;@Html.DisplayFor(m =&gt; Model.Results[i].SomeDisplayValue1)&lt;/td&gt;
              &lt;td&gt;@Html.DisplayFor(m =&gt; Model.Results[i].SomeDisplayValue2)&lt;/td&gt;
          &lt;tr&gt;
      }
      &lt;/table&gt;
      &lt;button type="submit"&gt;Do Something With Selected Results&lt;/button&gt;
  }
  </code></pre>