title: ASP.NET MVC 4 pass DB data back to view
body: <p>I am trying to create a View that accepts a name, does a lookup, and spits back a recordset to the page.  </p>
  
  <h2>The Model:</h2>
  
  <pre><code>public class User
  {
      [Required]
      [DisplayName("ID")]
      public int ID { get; set; }
  
      [DisplayName("FirstName")]
      public string FirstName { get; set; }
  
      [DisplayName("LastName")]
      public string LastName { get; set; }
  
      [DisplayName("FavouriteMovie")]
      public string FavouriteMovie { get; set; }
  }
  </code></pre>
  
  <h2>The View:</h2>
  
  <pre><code> @{
       ViewBag.Title = "Index";
  }
  @model MVCDatabaseFormTest.Models.User
  
  
  MainForm
  
  
  @Html.BeginForm();
  
  This is the form
  @Html.Label("Enter search for first name:");
  
  @Html.TextBoxFor(model =&gt; model.FirstName);
  
  &lt;input type="submit" value="Search" name="FormCollector1.PostBackButton" id="Search" /&gt;
  
  &lt;br /&gt;
  
  Display a title found:
  
  @Html.LabelFor(m =&gt; m.FirstName);&lt;br/&gt;
  @Html.LabelFor(m =&gt; m.LastName);&lt;br/&gt;
  @Html.LabelFor(m =&gt; m.FavouriteMovie);&lt;br/&gt;
  </code></pre>
  
  <hr>
  
  <p>The Controller:</p>
  
  <pre><code>    [HttpPost]
      public ActionResult Index(User user1)
      {
          string connectionString = "Data Source=cl02db01.cluster01.thindata.net; Initial Catalog=dev_backupDatabase_v3;Persist Security Info=True;User ID=ProductionMonitoringDashboard;Password=f3sf3sdf33;MultipleActiveResultSets=True";
  
          string info = user1.FirstName; // check value
  
          User u = new User();
  
          List&lt;User&gt; data = new List&lt;User&gt;();
          using (SqlConnection conn = new SqlConnection(connectionString))
          {
              conn.Open();
              using (SqlCommand command = new SqlCommand("select id,firstname,lastname,movie from _jterhofstede_TestMVC (nolock) where firstname like '%" + info + "%'", conn))
              {
                  SqlDataReader reader = command.ExecuteReader();
                  while (reader.Read())
                  {
                      u.ID = reader.GetInt32(0);
                      u.FirstName = reader["FirstName"].ToString();
                      u.LastName = reader["LastName"].ToString();
                      u.FavouriteMovie = reader["movie"].ToString();
                      data.Add(u);
  
                  }
              }
  
  
          }
  
          return View(u);
  </code></pre>
  
  <hr>
  
  <p>The Results:</p>
  
  <p>The code accepts a first name, and finds the results in the db table, and by checking with the debugger, it is being loaded successfully into the variable u, and I am passing it back to the view.</p>
  
  <p>However, the view does not display the results. I would actually ideally like to write the records out to the page (sort of like a GridView, but simpler).  I must be missing something simple but I cannot identify what it is.</p>