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;
}
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