Can anyone show me how to loop through my table in my view?
The rows I have are: disease_id, disease, and remedy.
My DB Query
I tried to get all the results into a list, so i can loop through them in the view.
SqlCommand cmd = new SqlCommand("SELECT * FROM disease", m_DBCon);
m_Reader = cmd.ExecuteReader();
List<string> record = new List<string>();
while (m_Reader.Read())
{
string s = Convert.ToString(m_Reader[0]);
record.Add(s);
}
ViewBag.Record = record;
My View
I want to loop the database records in a table
@{
ViewBag.Title = "Dashboard";
List<string> record = ViewBag.Record;
}
I keep getting an empty string. I thought I would have to do something like s[“disease”], s[“remedy”}, but s is a string so it doesn’t have all those fields.
@{
foreach (string s in record)
{
<tr>
<td>@s</td>
</tr>
}
}
Can anyone please give me some advice 🙂
I’d first want to point out that it’s probably easier to create a DiseaseResult class first.
Such that :
that makes things a little bit easier. Now you can say:
Afterward in your View: