title: Asp.Net MVC WebGrid paging...is there an alternative to putting data in session?
body: <p>I finally got my MVC WebGrid paging to work and my drop downlist box criteira to maintain state!!  I did this by putting the result data and criteria data in session for when the 'next page' is click on the grid and everything could be re-populated.  Is there another, more appropriate way to do this without loading up a bunch of session varables?</p>
  
  <p>HERE IS MY CONTROLER CODE:</p>
  
  <pre><code>    [HttpPost]
      public ActionResult Results(TestCriteriaConsolidated form) 
      { 
          List&lt;TestCriteriaResults&gt; gridResults = new List&lt;TestCriteriaResults&gt;();
          gridResults.Add(new TestCriteriaResults { Value1 = "one", Value2 = "one", Value3 = "three", Value4 = "Four" });
          gridResults.Add(new TestCriteriaResults { Value1 = "one", Value2 = "two", Value3 = "three", Value4 = "four" });
          gridResults.Add(new TestCriteriaResults { Value1 = "one", Value2 = "two", Value3 = "three", Value4 = "four" });
          gridResults.Add(new TestCriteriaResults { Value1 = "one", Value2 = "two", Value3 = "three", Value4 = "four" });
          gridResults.Add(new TestCriteriaResults { Value1 = "one", Value2 = "two", Value3 = "three", Value4 = "four" });
  
          Session["GridResults"] = gridResults;
          Session["SelectedYear"] = form.testCriteria.Year;
          form.testCriteriaResults = gridResults;             
  
          return View(form); 
      }
  
      [HttpGet]
      public ActionResult Results()
      {
          List&lt;TestCriteriaResults&gt; gridResults = ((List&lt;TestCriteriaResults&gt;) Session["GridResults"]);
          TestCriteria criteria = new TestCriteria() { Year = Session["SelectedYear"].ToString() };
          TestCriteriaConsolidated combined = new TestCriteriaConsolidated { testCriteriaResults = gridResults, testCriteria = criteria };
  
          return View(combined);
      }  
  </code></pre>
  
  <p>HERE IS CODE FROM MY PARIAL VIEW:</p>
  
  <pre><code>@using (Html.BeginForm("Results", "Home"))
  {
   &lt;div style="float:left"&gt;
                  &lt;div style="font-weight:bold"&gt;                
                      @Html.LabelFor(m =&gt; m.testCriteria.Year)
                  &lt;/div&gt;
                  &lt;div style="width:110px; "&gt;   
                       @Html.DropDownListFor(m =&gt; m.testCriteria.Year, new SelectList(new[] 
                       { 
  
                          new SelectListItem{ Text="All Years", Value=" " },
                          new SelectListItem{ Text="2010", Value="2010" },
                          new SelectListItem{ Text="2009", Value="2009" },
                          new SelectListItem{ Text="2008", Value="2008" },
                          new SelectListItem{ Text="2007", Value="2007" },
                          new SelectListItem{ Text="2006", Value="2006" }
  
                       }, "Value", "Text", "1"), new { @style = "width: 100px; margin-top: 5px" })          
                  &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div style="float:left"&gt;
          &lt;div&gt;                
              &amp;nbsp;
          &lt;/div&gt;
          &lt;div style="width:100px; "&gt;                                
                  &lt;input type="submit"  value="Results" /&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>}</p>
  
  <p>HERE IS MY VIEW CODE TO DISPLAY THE GRID WITH RESULTS:</p>
  
  <pre><code>@model MvcApplication1.Models.TestCriteriaConsolidated
  
  
         @{
  ViewBag.Title = "Index";
  Layout = "~/Views/Shared/_Layout.cshtml";
  }
  
  
  @if (Model == null || Model.testCriteriaResults == null)
  {
  
  }
  else
  {
  
  
  var grid = new WebGrid(Model.testCriteriaResults,canPage: true, rowsPerPage: 2);
  @grid.Pager(WebGridPagerModes.Numeric);     
  
  
      &lt;div id="grid"&gt;
          @grid.GetHtml()
  
      &lt;/div&gt;
  }
  </code></pre>