title: Losing all view model data when using paged list
body: <p>Just finished adding paging to my view using the <a href="https://github.com/TroyGoode/PagedList" rel="nofollow">Paged List</a> plugin and I am able to bring data back for one page only. If you click "Next" or any other page number available the page will resubmit itself and you wind up with a blank form. </p>
  
  <p>I stepped through the code and saw that the view model object being passed back to the controller is completely null. Does anyone know a way around this (ie retaining the view model data when navigating to a different paged list page)?</p>
  
  <p><strong>View Model</strong></p>
  
  <pre><code>public class ZipCodeIndex
  {
      [DisplayName("Zip Code")]
      public string searchZip { get; set; }
      public IEnumerable&lt;SelectListItem&gt; StateCodes { get; set; }
      [DisplayName("Effective on this date")]
      public string searchDate { get; set; }
      [DisplayName("State")]
      public string searchState { get; set; }
      [DisplayName("Territory")]
      public string searchTerritory { get; set; }
      [DisplayName("New Territory")]
      public string newTerritory { get; set; }
      [DisplayName("New Description")]
      public string newDescription { get; set; }
      [DisplayName("New Effective Date")]
      public string newEffectiveDate { get; set; }
  
      public IPagedList&lt;ZipCodeTerritory&gt; pagedTerritoryList { get; set; }
      public List&lt;ZipCodeTerritory&gt; zipCodeTerritory { get; set; }
  
      public ZipCodeIndex() 
      {
          zipCodeTerritory = new List&lt;ZipCodeTerritory&gt;();
          SetStateCodes();
      }
  
      //Set state code drop down list
      private void SetStateCodes()
      {
          AgentResources db = new AgentResources();
          StateCodes = (from z in db.ZipCodeTerritory
                        select z.StateCode).Select(x =&gt; new SelectListItem
                        {
                            Text = x,
                            Value = x
                        }).Distinct().ToList();
  
          db.Dispose();
      }
  }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>    public ViewResult Index(int? page, string errorMessage = "", ZipCodeIndex search = null)
      {
          //set Paged List counter variables
          int pageNumber = page ?? 1;
          int pageSize = 300;
  
          //If TempData conntains information method call came from another controller action
          if (TempData.Count &gt; 0)
          {
              //Instantiate ZipCodeIndex view model object if it exists
              search = (ZipCodeIndex)TempData["ZipCodeIndexData"];
  
              //Clear out previous search results
              search.zipCodeTerritory.Clear();
          }
  
          //Proceed with search
          try
          {
              //If search criteria is null page is loading for the first time so send blank view 
              if (String.IsNullOrWhiteSpace(search.searchZip) &amp;&amp;
                  String.IsNullOrWhiteSpace(search.searchDate) &amp;&amp;
                  String.IsNullOrWhiteSpace(search.searchState))
              {
                  if (!string.IsNullOrWhiteSpace(search.searchTerritory))
                  {
                      ViewBag.ErrorMessage = "State or Zip Code required for search.";
                  }
  
                  //Convert list to IPagedList for pagining on Index
                  search.pagedTerritoryList = search.zipCodeTerritory.ToPagedList(pageNumber, pageSize);
  
                  return View(search);
              }
  
              //Determine if necessary search criteria has been sent
              if (String.IsNullOrWhiteSpace(search.searchZip) &amp;&amp; String.IsNullOrWhiteSpace(search.searchState))
              {
                  ViewBag.ErrorMessage = "Either State or Zip Code Must be Specified";
  
                  //Convert list to IPagedList for pagining on Index
                  search.pagedTerritoryList = search.zipCodeTerritory.ToPagedList(pageNumber, pageSize);
  
                  return View(search);
              }
  
              DateTime effectiveDate;
  
              //Convert date string to DateTime type
              if (String.IsNullOrWhiteSpace(search.searchDate))
              {
                  effectiveDate = DateTime.MinValue;
              }
              else
              {
                  effectiveDate = Convert.ToDateTime(search.searchDate);
              }
  
              //Conduct search by State Code/Date alone
              if (String.IsNullOrWhiteSpace(search.searchZip))
              {
                  if (string.IsNullOrWhiteSpace(search.searchTerritory))
                  {
                      search.zipCodeTerritory = (from z in db.ZipCodeTerritory
                                                 where z.StateCode.Equals(search.searchState) &amp;&amp;
                                                       z.EffectiveDate &gt;= effectiveDate
                                                 select z).ToList();
                  }
                  else
                  {
                      search.zipCodeTerritory = (from z in db.ZipCodeTerritory
                                                 where z.StateCode.Equals(search.searchState) &amp;&amp;
                                                       z.IndDistrnId.Equals(search.searchTerritory) &amp;&amp;
                                                       z.EffectiveDate &gt;= effectiveDate
                                                 select z).ToList();
                  }
  
                  //Convert list to IPagedList for pagining on Index
                  search.pagedTerritoryList = search.zipCodeTerritory.ToPagedList(pageNumber, pageSize);
  
              }
  
              return View(search);
      }
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre><code>@model Monet.ViewModel.ZipCodeIndex
  
  @{
      ViewBag.Title = "Zip Code Territory Search";
      Layout = "~/Views/Shared/_Layout.cshtml";
  }
  
  @using PagedList.Mvc;
  @using PagedList;
  
  &lt;h2&gt;Zip Code Territory&lt;/h2&gt;
  
      @using (Html.BeginForm("Index", "ZipCodeTerritory", FormMethod.Post))
      {
          &lt;div class="error" id="searchErrors"&gt;
              @ViewBag.ErrorMessage            
          &lt;/div&gt;    
          &lt;br/&gt;
          &lt;div id="searchBox" class="boxMe"&gt;
              &lt;div id="zipBox"&gt;
                  @Html.Raw("Zip Code")
                  @Html.TextAreaFor(model =&gt; model.searchZip, new { style = "width: 300px;", placeholder = "Enter up to 35 comma separated zip codes" }) 
              &lt;/div&gt;
              &lt;div id="dateBox"&gt;
                  @Html.LabelFor(model =&gt; model.searchDate)
                  @Html.TextBoxFor(model =&gt; model.searchDate, new { style="width: 80px;"})
                  &lt;div id="terrBox"&gt;
                       @Html.LabelFor(model =&gt; model.searchTerritory)
                       @Html.TextBoxFor(model =&gt; model.searchTerritory, new { style = "width: 30px;padding-left:10px;", maxLength = 3 })                   
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div id="stateBox"&gt;
                  @Html.LabelFor(model =&gt; model.searchState)
                  @Html.DropDownListFor(model =&gt; model.searchState, Model.StateCodes, "  ")
                  &lt;button type="submit" id="SearchButton"&gt;Search&lt;/button&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div style="clear: both;"&gt;&lt;/div&gt;
      }
  
  &lt;br/&gt;
  @Html.ActionLink("Create New", "Create")
  &lt;br/&gt;
  &lt;br/&gt;
  &lt;div class="error" id="updateErrors"&gt;
      @ViewBag.UpdateAction            
  &lt;/div&gt;
  @if (Model.zipCodeTerritory.Count &gt; 0)
  {
      using (Html.BeginForm("Update", "ZipCodeTerritory", FormMethod.Post))
      {
          @Html.HiddenFor(model =&gt; model.searchZip)
          @Html.HiddenFor(model =&gt; model.searchDate)
          @Html.HiddenFor(model =&gt; model.searchState)
  
          &lt;div id="cloneBox"&gt;
              @Html.LabelFor(model =&gt; model.newTerritory)
              @Html.TextBoxFor(model =&gt; model.newTerritory, new { style = "width: 30px;padding-left:10px;", maxLength = 3 })
              @Html.LabelFor(model =&gt; model.newDescription)
              @Html.TextBoxFor(model =&gt; model.newDescription, new { style = "width: 250px;padding-left:10px;", maxLength = 30 })  
              @Html.LabelFor(model =&gt; model.newEffectiveDate)     
              @Html.TextBoxFor(model =&gt; model.newEffectiveDate, new { style = "width: 80px;padding-left:10px;" })   
          &lt;/div&gt;
          &lt;br/&gt;
          &lt;div id="buttonDiv"&gt;
              &lt;button type="submit" id="CloneButton" name="button" value="clone"&gt;Update Selected Items&lt;/button&gt;
              &lt;button type="submit" id="deleteButton" name="button" value="delete"&gt;Delete Selected Items&lt;/button&gt;            
          &lt;/div&gt;
          &lt;div id="pagingDiv"&gt;
              @Html.PagedListPager(Model.pagedTerritoryList, page =&gt; Url.Action("Index", new { page })) 
          &lt;/div&gt;
          &lt;table id="thetable" class="tablesorter" &gt;
              &lt;thead&gt;
                  &lt;th&gt;@Html.CheckBox("SelectAll")&lt;/th&gt;
                  &lt;th&gt;Channel&lt;/th&gt;
                  &lt;th&gt;Territory&lt;/th&gt;
                  &lt;th&gt;Description&lt;/th&gt;
                  &lt;th&gt;State&lt;/th&gt;
                  &lt;th&gt;Zip&lt;/th&gt;
                  &lt;th&gt;Effective&lt;/th&gt;
                  &lt;th&gt;End Date&lt;/th&gt;
                  &lt;th&gt;Last Update By&lt;/th&gt;
                  &lt;th&gt;Last Update Date&lt;/th&gt;
                  &lt;th&gt;&lt;/th&gt;
              &lt;/thead&gt;
              &lt;tbody id="tableBody"&gt;
                  @for (int i = 0; i &lt; Model.pagedTerritoryList.Count; i++)
                  {
                      &lt;tr&gt;
                          &lt;td&gt;
                              @Html.CheckBoxFor(model =&gt; model.pagedTerritoryList[i].Update)
                              @Html.HiddenFor(model =&gt; model.pagedTerritoryList[i].Update)
                          &lt;/td&gt;
                          &lt;td&gt;
                              @Html.DisplayFor(model =&gt; model.pagedTerritoryList[i].ChannelCode)
                              @Html.HiddenFor(model =&gt; model.pagedTerritoryList[i].ChannelCode)
                          &lt;/td&gt;
                          &lt;td&gt;
                              @Html.DisplayFor(model =&gt; model.pagedTerritoryList[i].IndDistrnId)
                              @Html.HiddenFor(model =&gt; model.pagedTerritoryList[i].IndDistrnId)
                          &lt;/td&gt;
                          &lt;td&gt;
                              @Html.DisplayFor(model =&gt; model.pagedTerritoryList[i].DrmTerrDesc)
                              @Html.HiddenFor(model =&gt; model.pagedTerritoryList[i].DrmTerrDesc)
                          &lt;/td&gt;
                          &lt;td&gt;
                              @Html.DisplayFor(model =&gt; model.pagedTerritoryList[i].StateCode)
                              @Html.HiddenFor(model =&gt; model.pagedTerritoryList[i].StateCode)
                          &lt;/td&gt;
                          &lt;td&gt;
                              @Html.DisplayFor(model =&gt; model.pagedTerritoryList[i].ZipCode)
                              @Html.HiddenFor(model =&gt; model.pagedTerritoryList[i].ZipCode)
                          &lt;/td&gt;
                          &lt;td&gt;
                              @Html.DisplayFor(model =&gt; model.pagedTerritoryList[i].EffectiveDate)
                              @Html.HiddenFor(model =&gt; model.zipCodeTerritory[i].EffectiveDate)
                          &lt;/td&gt;
                          &lt;td&gt;
                              @Html.DisplayFor(model =&gt; model.pagedTerritoryList[i].EndDate)
                              @Html.HiddenFor(model =&gt; model.pagedTerritoryList[i].EndDate)
                          &lt;/td&gt;
                          &lt;td&gt;
                              @Html.DisplayFor(model =&gt; model.pagedTerritoryList[i].LastUpdateId)
                              @Html.HiddenFor(model =&gt; model.pagedTerritoryList[i].LastUpdateId)
                          &lt;/td&gt;
                          &lt;td&gt;
                              @Html.DisplayFor(model =&gt; model.pagedTerritoryList[i].LastUpdateDate)
                              @Html.HiddenFor(model =&gt; model.pagedTerritoryList[i].LastUpdateDate)
                          &lt;/td&gt;
                          &lt;td&gt;
                              @Html.ActionLink("Edit", "Edit", new { id = Model.pagedTerritoryList[i].Id })
                              @Html.HiddenFor(model =&gt; model.pagedTerritoryList[i].Id)
                          &lt;/td&gt;
                      &lt;/tr&gt;
                  }
              &lt;/tbody&gt;
          &lt;/table&gt;
      }
  }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>Found <a href="http://pastebin.com/P0FuyUte" rel="nofollow">this article</a> and altered my <code>@Html.PagedListPager</code> accordingly, however no luck (still passing a <code>null</code> view model object and now even passing null <code>page</code> parameter...)</p>
  
  <pre><code>    &lt;div id="pagingDiv"&gt;
          @Html.PagedListPager(Model.pagedTerritoryList, page =&gt; Url.Action("Index", new RouteValueDictionary()
              {
                  { "Page", Page},
                  { "search", Model }
              }), PagedListRenderOptions.PageNumbersOnly)
      &lt;/div&gt;
  </code></pre>