如何从控制器显示数据表数据到视图表格式? [英] How to display datatable data from controller to view table format?
问题描述
Hello Everyone,
我最近在asp.net中使用MVC2 Sujata。我必须从模型中的sql检索表数据并传递给通过控制器查看。但是我面临着内部问题查看页面。
请查看我在CRUDModel,SaveController和Display View中的代码如下。
CRUDModel.cs
public DataTable getAllEmployee()
{
DataTable dt = new DataTable();
string strConString = @ 数据源= 。;初始目录= Sujata;集成安全性=真;
使用(SqlConnection con = new SqlConnection(strConString))
{
con.Open();
SqlCommand cmd = new SqlCommand( 选择*来自员工,con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
return dt;
// 抛出新的NotImplementedException();
}
SaveController.cs内的代码
public ActionResult ShowRecord()
{
CRUDModel crud = new CRUDModel();
DataTable dt = new DataTable();
dt = crud.getAllEmployee();
return 查看( 显示跨度>,dt.Rows);
}
Display.aspx上的代码查看为
<%@ Page 语言 = C# 继承 = System.Web.Mvc.ViewPage< MvcCRUDApplication.Models.CRUDModel> %>
< !DOCTYPE html PUBLIC - / / W3C // DTD XHTML 1.0 Transitional // EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd >
< html xmlns = http://www.w3.org/1999/xhtml >
< head runat = 服务器 >
< title > 显示< / title >
< / head >
< 正文 >
< h2 > 显示< / h2 >
< 输入 cl屁 = 按钮 类型 = 按钮 value = Clickme onclick = location.href ='<% = Url.Action( ShowRecord, 保存 )%>' / >
< div >
< table border = 1 >
< thead >
< tr >
< td >
名称
< / td >
< td >
员工ID
< / td >
< td >
指定
< / td >
< td > 部门< / td >
< / tr >
< / thead >
<% foreach(System.Data.DataRow dr in Model.Rows)
{%>
< tr >
< td > <% = dr [ 名称]。ToString()%> < / td >
< td > < span class =code-pagedirective><% = dr [ EmployeeId ]的ToString()%> < / td >
< td > < span class =code-pagedirective><% = dr [ Desig ]的ToString()%> < / td >
< td > < span class =code-pagedirective><% = dr [ 部门 ]的ToString()%> < / td >
< / tr >
<%}%>
< / table >
< / div >
< / body >
< / html >
视图页面上面的代码给出了以下错误
编译器错误消息:CS1061: ' MvcCRUDApplication.Models.CRUDModel'不包含的定义 ' 行',没有扩展方法' Rows'接受'类型的第一个参数可以找到 MvcCRUDApplication.Models.CRUDModel'(您是否缺少使用指令或汇编参考?)
请帮帮我...
感谢
从你的控制器中你将数据表的Rows属性作为你的模型返回,这是类型System.Data.DataRowCollection
DataTable.Rows属性(System.Data) [ ^ ]
DataRowCollection类(System.Data) [ ^ ]
但是您的视图页面显示了它正在寻找CRUDModel类型的东西。因此,请将视图更改为期望System.Data.DataRowCollection的模型,并将代码更改为;
<% foreach (System.Data.DataRow dr in Model)
但是我也建议你放弃MVC2和aspx页面,这是非常古老的技术,我将转向更新版本的MVC和Razor引擎。
Hello Everyone,
I am Sujata recently using MVC2 in asp.net.Here i have to retrieve table data from sql inside the model and pass to view through controller.But I am facing problem inside the view page.
Please check my code inside CRUDModel,SaveController and Display View is as follows.
CRUDModel.cs
public DataTable getAllEmployee()
{
DataTable dt = new DataTable();
string strConString =@"Data Source=.;Initial Catalog=Sujata;Integrated Security=True";
using (SqlConnection con = new SqlConnection(strConString))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from Employee", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
return dt;
//throw new NotImplementedException();
}
Code inside SaveController.cs
public ActionResult ShowRecord()
{
CRUDModel crud = new CRUDModel();
DataTable dt = new DataTable();
dt=crud.getAllEmployee();
return View("Display",dt.Rows);
}
Code on Display.aspx View as
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcCRUDApplication.Models.CRUDModel>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Display</title>
</head>
<body>
<h2>Display</h2>
<input class="button" type="button" value="Clickme" onclick="location.href='<%=Url.Action("ShowRecord", "Save")%>'" />
<div>
<table border="1">
<thead>
<tr>
<td>
Name
</td>
<td>
Employee ID
</td>
<td>
Designation
</td>
<td>Department</td>
</tr>
</thead>
<% foreach (System.Data.DataRow dr in Model.Rows)
{ %>
<tr>
<td> <%=dr["Name"].ToString() %> </td>
<td><%=dr["EmployeeId"].ToString()%> </td>
<td><%=dr["Desig"].ToString()%> </td>
<td><%=dr["Dept"].ToString()%> </td>
</tr>
<% } %>
</table>
</div>
</body>
</html>
The above code on view page gives following error
Compiler Error Message: CS1061: 'MvcCRUDApplication.Models.CRUDModel' does not contain a definition for 'Rows' and no extension method 'Rows' accepting a first argument of type 'MvcCRUDApplication.Models.CRUDModel' could be found (are you missing a using directive or an assembly reference?)
Please help me ...
thanks
From your controller you're returning the Rows property of a datatable as your model, which is something of type System.Data.DataRowCollection
DataTable.Rows Property (System.Data)[^]
DataRowCollection Class (System.Data)[^]
However your view page says it is looking for something of type CRUDModel. So change the view to expect a model of System.Data.DataRowCollection instead and change the code to;
<% foreach (System.Data.DataRow dr in Model)
However I'd also advise you to ditch MVC2 and aspx pages, that is very old tech, I'd move to a more up-to-date version of MVC and the Razor engine.
这篇关于如何从控制器显示数据表数据到视图表格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!