title: Razor Pages - Foreach loop through list to get data to display on razor view
body: <p>I can't get the data to display on the razor view. I am currently getting the data and filling my object in the <strong>DataAccessLayer.GetData()</strong> function. My objects list gets filled and stored. However my objects list count is 0 on the razor view therefore it shows nothing. Am I <strong>"newing"</strong> up my Object correctly on the view? This is the first time using the razor pages template so it's a learning curve. </p>
  
  <p><strong>Razor Page:</strong></p>
  
  <pre><code>&lt;form method="post" asp-page-handler="GetData"&gt;
      &lt;input id="txtSearch" name="search" /&gt;
      &lt;button class="btn btn-lg btn-dark" id="btnSubmit" name="Submit" /&gt;
  &lt;/form&gt;
  
  
  @{ List&lt;ObjectsModel&gt; objects = new List&lt;ObjectsModel&gt;();
  
      @foreach (var record in objects)
      {
          &lt;tr&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; record.COLUMN1)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; record.COLUMN2)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; record.COLUMN3)
              &lt;/td&gt;
          &lt;/tr&gt;
      }
  }
  </code></pre>
  
  <p><strong>PageModel:</strong></p>
  
  <pre><code> public class ObjectsModel : PageModel
  {
      public string COLUMN1 { get; set; }
      public string COLUMN2 { get; set; }
      public string COLUMN3 { get; set; }
  
      public void OnGet()
      {
  
      }
  
      public void OnPostGetData(string search)
      {
  
          List&lt;ObjectsModel&gt; objects = new List&lt;ObjectsModel&gt;();
  
          DataAccessLayer.GetData(search, out objects);
          return;
      }
  }
  </code></pre>
  
  <p><strong>DataAccessLayer:</strong></p>
  
  <pre><code>public class DataAccessLayer
      {
          public static bool GetData(string ID, out List&lt;ObjectsModel&gt; objects)
          {
              string connectionString = "";
              objects = new List&lt;ObjectsModel&gt;();
  
              try
              {
                  SqlConnection connection = new SqlConnection(connectionString);
                  connection.Open();
  
                  SqlCommand command = connection.CreateCommand();
  
                  command.CommandType = System.Data.CommandType.Text;
  
                  command.CommandText = @"SELECT top 50 COLUMN1, COLUMN2, COLUMN3
                                           "FROM Table";
  
  
                  SqlDataReader reader = command.ExecuteReader();
  
                  while (reader.Read())
                  {
                      ObjectsModel item = new ObjectsModel();                
                      item.COLUMN1 = reader.GetValue(0).ToString().Trim();
                      item.COLUMN2 = reader.GetValue(1).ToString().Trim();
                      item.COLUMN3 = reader.GetValue(2).ToString().Trim();
  
                      objects.Add(item);
                  }
  
                      reader.Close();
                      reader = null;
                      command = null;         
                      connection.Close();
                      connection = null;
  
                      return true;
              }
              catch (Exception exc)
              {
                  exc.ToString();
                  return false;
              }
          }
      }
  </code></pre>