C# 如何通过 DataReader/DataTable 进行查看?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17869366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I pass DataReader/DataTable to view?
提问by Builder
I am new to MVC and trying to write a simple MVC 3 application which reads customer data inside a class in model and return it to view using controller. Reader shows it has rows but when load to table and pass to view as model, it is null. I need a simple solution to pass either a DataTable
to view or DataReader
to view where I can convert to DataTable
.
我是 MVC 的新手,正在尝试编写一个简单的 MVC 3 应用程序,该应用程序读取模型类中的客户数据并使用控制器将其返回给视图。Reader 显示它有行,但是当加载到表并作为模型传递给视图时,它为空。我需要一个简单的解决方案来传递 aDataTable
以查看或DataReader
查看可以转换为DataTable
.
Here is the code:
这是代码:
namespace MvcApplication3.Models
{
public class Customer
{
public DataTable GetCustomers(OdbcDataReader rds)
{
String ConString = "DSN=Northwind;Uid=;Pwd=;";
String SQL = "SELECT * FROM CUSTOMERS";
DataTable tbl = new DataTable();
using (OdbcConnection con = new OdbcConnection(ConString))
{
con.Open();
OdbcCommand cmd = new OdbcCommand(SQL, con);
rds = cmd.ExecuteReader();
}
}
}
}
namespace MvcApplication3.Controllers
{
public class DefaultController : Controller
{
//
// GET: /Default1/
public ActionResult Index()
{
Customer cs = new Customer();
OdbcDataReader rd = new OdbcDataReader();
cs.GetCustomers(rd);
DataTable tb = new DataTable();
tb.Load(rd);
return View(tb);
}
}
}
采纳答案by Surya Deepak
A view in mvc is always associated with a strongly typed model I.e a class..so in your case..just create a class with a list..while fetching data insert that data into a list and pass that class into the view..
mvc 中的视图始终与强类型模型相关联,即类..所以在您的情况下..只需创建一个带有列表的类..同时获取数据将该数据插入列表并将该类传递到视图中..
class A {
public string name{get;set;}
public int age{get;set;}
}
//code when fetching data
//this list should be in the class where you fetch the data
Public list<A> list=new list<A>();
while(datareader.read)
{
A Aobj=new A();
String name=reader['name'].tostring();
int age =Convert.ToInt(reader['age']);
A.name=name;
A.age=age;
list.add(A);
}//end of while loop
//action resut
public ViewResult yourview(){
Return View(list);
}
//now you can access the data from the view.
By....
@model IEnumerable<A>
//here A is your class name
@foreach(var data in Model){
<div>@data.age</div>
}
Sorry for the code format... posted from mobile
抱歉代码格式...从手机发布
回答by Cam Bruce
Do not pass a DataReader
into a view - convert it to a disconnected object/model first.
不要将 a 传递给DataReader
视图 - 首先将其转换为断开连接的对象/模型。
If you are using Razor, add this to the top of your .cshtml page to tell it what type of model to expect:
如果您使用 Razor,请将其添加到 .cshtml 页面的顶部以告诉它期望的模型类型:
@model System.Data.DataTable
回答by ManiKandan Selvanathan
1.Pass datatable into the view.
1.将数据表传递到视图中。
public ActionResult my()
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from table",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return View(dt);
}
2.Accept the datatable in the view
2.在视图中接受数据表
@model System.Data.DataTable
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>my</title>
</head>
<body>
<div>
No of rows= @Model.Rows.Count
</div>
</body>
</html>
Try it out :) (y)
试试看:) (y)
回答by Mathesh Waran
**Copy paste as it is to your view**
@model System.Data.DataTable
@using System.Data;
<h2>Report</h2>
<table>
<thead>
<tr>
@foreach (DataColumn col in Model.Columns)
{
<th>@col.ColumnName</th>
}
</tr>
</thead>
<tbody>
@foreach (DataRow row in Model.Rows)
{
<tr>
@foreach (DataColumn col in Model.Columns)
{
<td>@row[col.ColumnName]</td>
}
</tr>
}
</tbody>
</table>
回答by racsonp
for the controller :
对于控制器:
public ActionResult Index()
{
DataTable dt = GetData();
return View(dt);
}
Then in the view :
然后在视图中:
@model System.Data.DataTable
@using System.Data;
.................. .............. ........
…………………………
<table class="table table-hover">
<thead>
<tr>
@foreach (DataColumn col in Model.Columns)
{
<th>@col.ColumnName</th>
}
</tr>
</thead>
<tbody>
@foreach (DataRow row in Model.Rows)
{
<tr>
@foreach (DataColumn col in Model.Columns)
{
<td>@row[col.ColumnName]</td>
}
</tr>
}
</tbody>
</table>
回答by G Samba
Copy paste this in your view and one thing what you want display expected details like based on Index;
将此复制粘贴到您的视图中,并且您希望显示基于索引的预期详细信息;
@using System.Data;
@model DataTable
<h2>Report</h2>
<table>
<thead>
<tr>
<th>@Model.ColumnName[1]</th>
</tr>
</thead>
<tbody>
@foreach (DataRow row in Model.Rows)
{
<tr>
<td>@row[1]</td>
</tr>
}
</tbody>
</table>