简体   繁体   English

如何在C#MVC 3的视图中循环数据库记录

[英]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.

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. 我以为我必须做类似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_iddiseaseremedy ,是他们的值或列? 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.

相关问题 如何从C#MVC中的记录获取数据 - How do i get data from records in C# MVC 如何连接到数据库并在 C# 中循环记录集? - How do I connect to a database and loop over a recordset in C#? 如何遍历和编辑在 ASP.NET MVC C# 中选中复选框的数据库实体表中的记录 - How to loop through and edit records from database entity table that has a checkbox checked in ASP.NET MVC C# 使用for循环将数据库记录存储到数组中 C# - Storing database records into array with a for loop | C# 如何使用Winforms C#在数据库多个表中插入记录 - How do I Insert records in database multiple Tables using winforms c# 如何编写 SQL 以使用 C# 从 Access 数据库中检索单个日期的记录 - How do I write the SQL to retrieve records for a single date from Access database using C# 如何在 C# ASP.NET MVC 的一个视图中创建多个条目? - How do I create multiple entries in one view in C# ASP.NET MVC? 使用mvc2和c#,如何从视图中调用控制器中的void方法 - Using mvc2 and c#, how do I call a void method in my controller from view 我如何在c#中循环一个函数? - How do I loop a function in c#? 如何使用C#和MVC 2从SQL数据库生成PDF / Excel文件? - How do I generate a PDF/Excel file from an SQL database using C# and MVC 2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM