title: WebGrid form posting in ASP.NET Web Pages Razor v2
body: <p>OK, typical problem:</p>
  
  <ul>
  <li>I have a table with 7884 of records (for now—but there will be lots...)</li>
  <li>I want to offer admins a quick way to monitor this table through the web.</li>
  <li>I also need to provide viewers the ability to specify some filtering to further narrow the results, as well as be able to say how many records to pull from the DB.</li>
  </ul>
  
  <p>I'm making my intro to ASP.NET web development and so I chose to use the (more straightforward) ASP.NET WebPages framework.</p>
  
  <p>I'm using the <code>System.Web.Helpers.WebGrid</code> to display the records and I like the fact that it automatically implements paging.</p>
  
  <p>I'm using a <code>&lt;form method="post"&gt;</code> to gather the filtering parameters, so that when the form  posted back, I can adjust my querying based on these values. I also assign these values to the form fields so that the user can see the posted parameters in use.</p>
  
  <p>However, I noticed that the page links generated by the <code>WebGrid</code> that are displayed in the page footer all the reference current page URL with a query fragment of the form <em><code>?page=n</code></em>.</p>
  
  <p>So, obviously, when the pages are navigated, I receive a request without my page form data...</p>
  
  <p><strong>What would be your recommended pattern to solved this simple issue and keeping the development of these pages short for the time being?</strong></p>
  
  <pre><code>@{
      Layout = "~/_TablePageLayout.cshtml";
      Page.Title = "Wala-Wala";
  
      var headlinePattern = Request.Form["headlinePattern"];
      var _headlinePattern = headlinePattern;
      if (string.IsNullOrWhiteSpace(headlinePattern)) {
          _headlinePattern = "%";
          headlinePattern = string.Empty;
      }
  
      var maxCount = Request.Form["maxCount"];
      int? _maxCount = maxCount.TryConvert&lt;int&gt;();
      if (_maxCount == null || _maxCount &lt; 0) {
          _maxCount = null;
          maxCount = string.Empty;
      }
  
      var database = Database.Open("Wala-Wala-DB");
      var query = _maxCount == null
          ? @"SELECT * FROM dbo.WalaWala WHERE Headline LIKE @1"
          : @"SELECT TOP(@0) * FROM dbo.WalaWala WHERE Headline LIKE @1";
  
      var dataSource = database.Query(query, _maxCount, _headlinePattern);
  
      var grid = new WebGrid(source: dataSource, rowsPerPage: 25, canPage: true);
      grid.SortColumn = "Id";
      grid.SortDirection = SortDirection.Descending;
  
      var rowStart = grid.PageIndex * @grid.RowsPerPage + 1;
      var rowEnd = grid.PageIndex * @grid.RowsPerPage + grid.Rows.Count;
  }
  
  &lt;h1&gt;@Page.Title&lt;/h1&gt;
  
  &lt;form method="post"&gt;
  &lt;fieldset&gt;
      &lt;legend&gt;Search&lt;/legend&gt;
      &lt;ol&gt;
          &lt;li&gt;
              &lt;label for="maxCount"&gt;Maximum:&lt;/label&gt;
              &lt;input type="text" id="maxCount" name="maxCount" value="@maxCount""/&gt;
          &lt;/li&gt;
          &lt;li&gt;
              &lt;label for="headlinePattern"&gt;Headline Pattern:&lt;/label&gt;
              &lt;input type="text" id="headlinePattern" name="headlinePattern" value="@headlinePattern""/&gt;
              &lt;input type="submit" value="Search" /&gt;
          &lt;/li&gt;
      &lt;/ol&gt;
  &lt;/fieldset&gt;
  &lt;/form&gt;
  
  &lt;p&gt;
      &lt;b&gt;@grid.TotalRowCount entries found. Showing results @rowStart to @rowEnd (page @(grid.PageIndex + 1)
          of @grid.PageCount).&lt;/b&gt;
  &lt;/p&gt;
  
  &lt;div&gt;
      @grid.GetHtml(
          tableStyle: "tableStyle",
          alternatingRowStyle: "alternatingRowStyle",
          mode: WebGridPagerModes.All,
          columns: new[] {
              grid.Column(columnName: "Id", style: "columnStyle"),
              grid.Column(columnName: "Headline", style: "columnStyle"),
              grid.Column(columnName: "DTS", style: "columnStyle"),
          }
      )
  &lt;/div&gt;
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/I5Ept.gif" alt="enter image description here"></p>
  
  <p><img src="https://i.stack.imgur.com/ZeHCH.gif" alt="enter image description here"></p>