title: How to display database records in asp.net mvc view
body: <p>Using ASP.NET MVC with C#, how do you pass some database records to a View and display them in table form?</p>
  
  <p>I need to know how I can transfer/pass some rows of records from a database that have been returned to an SqlDataReader object and pass that object to the View so I can display all the records contained by the object in the View using foreach.</p>
  
  <p>The following code is what I'm I'm trying to do. But its not working.</p>
  
  <p>The Controller:</p>
  
  <pre><code>public ActionResult Students()
  {
      String connectionString = "&lt;THE CONNECTION STRING HERE&gt;";
      String sql = "SELECT * FROM students";
      SqlCommand cmd = new SqlCommand(sql, connectionString);
  
      using(SqlConnection connectionString = new SqlConnection(connectionString))
      {
          connectionString.Open();
          SqlDataReader rdr = cmd.ExecuteReader();
      }
  
      ViewData.Add("students", rdr);
  
      return View();
  }
  </code></pre>
  
  <p>The View:</p>
  
  <pre><code>&lt;h1&gt;Student&lt;/h1&gt;
  
  &lt;table&gt;
      &lt;!-- How do I display the records here? --&gt;
  &lt;/table&gt;
  </code></pre>