Created
June 26, 2014 16:00
-
-
Save KyleGobel/6dc776242a23da9a591d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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