Using ASP.NET MVC with C#, how do you pass some database records to a View and display them in table form?
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.
The following code is what I'm I'm trying to do. But its not working.
The Controller:
public ActionResult Students()
{
String connectionString = "<THE CONNECTION STRING HERE>";
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();
}
The View:
<h1>Student</h1>
<table>
<!-- How do I display the records here? -->
</table>
Right-click the Models folder in the Solution Explorer window and the select the menu option Add, New Item. Select the Data category and select the ADO.NET Entity Data Model template. Give your data model the name MoviesDBModel. edmx and click the Add button.
Select Web from the left panel, choose ASP.NET Web Application, give a meaningful name of your project, then click on OK. After clicking on OK, one more window will appear. Choose Empty check on MVC and Web API checkbox and click on OK, as shown in the below screenshot.
1. First create a Model
that will hold the values of the record. for instance:
public class Student
{
public string FirstName {get;set;}
public string LastName {get;set;}
public string Class {get;set;}
....
}
2. Then load the rows from your reader to a list or something:
public ActionResult Students()
{
String connectionString = "<THE CONNECTION STRING HERE>";
String sql = "SELECT * FROM students";
SqlCommand cmd = new SqlCommand(sql, conn);
var model = new List<Student>();
using(SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while(rdr.Read())
{
var student = new Student();
student.FirstName = rdr["FirstName"];
student.LastName = rdr["LastName"];
student.Class = rdr["Class"];
....
model.Add(student);
}
}
return View(model);
}
3. Lastly in your View
, declare the kind of your model:
@model List<Student>
<h1>Student</h1>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Class</th>
</tr>
@foreach(var student in Model)
{
<tr>
<td>@student.FirstName</td>
<td>@student.LastName</td>
<td>@student.Class</td>
</tr>
}
</table>
If you dont have to use an sql reader would it not be easier to have the Controller like this.
Controller.cs
private ConnectContext db = new ConnectContext();
public ActionResult Index()
{
return View(db.Tv.ToList());
}
ConnectContext.cs
public class ConnectContext : DbContext
{
public DbSet<Student> Student{ get; set; }
}
This way your connection string will be in your web.config and the View + Model will remain the same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With