[英]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.
我的行是:
disease_id, disease, and remedy.
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;
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.
我以为我必须做类似s [“疾病”],s [“补救”}的事情,但s是一个字符串,所以它没有所有这些字段。
@{
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. 我首先要指出的是,首先创建一个DiseaseResult类可能更容易。 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? 这三个项目
disease_id
, disease
和remedy
,是他们的值或列? How many records do you have in desease
table? 你在
desease
表中有多少条记录? 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. 使用var而不是string允许从记录中提取的对象是隐式而非强烈的。 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 http://msdn.microsoft.com/en-us/library/bb383973.aspx
I hope this helps. 我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.