在我的 ASP MVC 应用程序中,我使用标准 SQL(而不是 Linq to SQL 或其他 ORM)来查询我的数据库。
我想将数据库结果传递给我的 View 并在我的 View 中迭代结果。但我不知道如何做到这一点。我见过的每个例子都传递一些字符串或使用 L2S。我想传递诸如嵌套 Hashtables 之类的东西,但我唯一能想到的是将 SqlDataReader 对象传递给 View ,但这听起来很糟糕。
我将如何将标准 SQL 查询的数据库结果显示到我的 View 中?我真的很想使用 Linq 或其他 ORM,但需求决定我们不这样做(不要问我为什么,我不明白)。我在 VB 中这样做。我会尽力转换提供的任何 C# 示例。
最佳答案
您可以为要传输的数据创建简单的类,然后从数据读取器手动填充 Controller 中的对象列表,然后将其传递给您的 View - 例如(C# 但这应该很容易转换)
// open your connection / datareader etc.
List<Customer> customers = new List<Customer>();
while(dataReader.Read())
{
Customer c = new Customer();
c.Id = dataReader.GetInt32(0);
c.Name = dataReader.GetString(1);
// etc (you might want to use string indexers instead of ints for the get methods)
customers.Add(c);
}
// close and dispose your datareader / connection etc as usual
return View("List", customers);
关于asp.net-mvc - 在没有 ORM 的情况下使用 ASP.NET MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/817733/