Skip to content

Instantly share code, notes, and snippets.

@KyleGobel
Created June 26, 2014 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KyleGobel/6dc776242a23da9a591d to your computer and use it in GitHub Desktop.
Save KyleGobel/6dc776242a23da9a591d to your computer and use it in GitHub Desktop.
@model List<User>
<!-- loop through all the users in our model, and display their name and age -->
@foreach(var user in Model)
{
<div>
<p>Users Name: @user.Name</p>
<p>Users Age: @user.Age</p>
</div>
}
public class User
{
public string Name {get; set;}
public int Age {get; set;}
}
public class MyTestController : Controller
{
//this is the action thta is run for /MyTest/
public ActionResult Index()
{
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
var command = new SqlCommand(connection)
command.CommandText = "SELECT * FROM Users";
var reader = command.ExecuteReader();
List<User> users = new List<User>();
while (reader.Read())
{
var user = new User
{
Name = reader["Name"];
Age = int.Parse(reader["Age"]);
};
users.Add(user);
}
}
//pass the populated List<User> back to the view for displaying
return View(users);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment