简体   繁体   中英

How do I Loop Database Records in the View of C# MVC 3

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 :

public class DiseaseResult
{
  public int DiseaseId { get; set; }
  public string Disease { get; set; }
  public string Remedy { get; set; }
}

that makes things a little bit easier. Now you can say:

public ActionResult Index()
{
    SqlCommand cmd = new SqlCommand("SELECT [disease_id], [disease], [remedy] FROM disease", m_DBCon);
    m_Reader = cmd.ExecuteReader();

    List<DiseaseResult> record = new List<DiseaseResult>();
    while (m_Reader.Read())
    {
        record.Add(new DiseaseResult() { DiseaseId = m_Reader[0], Disease = m_Reader[1], Remedy = m_Reader[2]);
    }

    return View(record);
}

Afterward in your View:

@model List<DiseaseResult>

@foreach(DiseaseResult item in Model)
{
   <div>@item.Disease</div>
}

The three items disease_id , disease and remedy , are they values or columns? How many records do you have in desease table? Seems that you are confusing "record" with collection of "fields values from different records". Try

string s = Convert.ToString(m_Reader[0]) + "," + Convert.ToString(m_Reader[1]) + "," + Convert.ToString(m_Reader[2]);

My view code for the same environment is as such:

<% foreach (var item in Model.Item)
{ 
%>
  <tr>
    <td>
      <%: item.Id %>
    </td>
    <td>
      <%: item.Name %>
    </td>
  </tr>
<%
}
%>

Using a var instead of string allows the object your pulling from record to be cast implicitly rather than strongly. I believe this is issue you're encountering in your code above. Check out the following link for more information:

http://msdn.microsoft.com/en-us/library/bb383973.aspx

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM