Hi Avnish,
There are two way to pass DataSet to view from Contrller
Passing the DataSet as Model:
Any object we can pass from cotroller in return View(Object) and we get this object by Model Variable. To get this object in View page we provide datatype of the object .In following example we pass DataSet object from controller as ds and in View @model System.Data.DataSet in Razor and in ASPX Inherits="System.Web.Mvc.ViewPage<System.Data.DataSet>"
In Controller:
public ActionResult Index()
{
ViewData["Message"] = "Welcome to Nice One Code";
DataSet ds = new DataSet();
DataTable dt = new DataTable("NiceTable");
dt.Columns.Add(new DataColumn("Col1", typeof(string)));
dt.Columns.Add(new DataColumn("Col2", typeof(string)));
dt.Columns.Add(new DataColumn("Col3", typeof(string)));
for (int i = 0; i < 3; i++)
{
DataRow row = dt.NewRow();
row["Col1"] = "col 1, row " + i;
row["Col2"] = "col 2, row " + i;
row["Col3"] = "col 3, row " + i;
dt.Rows.Add(row);
}
ds.Tables.Add(dt);//Better way get dataset from model class
return View(ds); //passing the DataSet as my Model
}
In View:
RAZOR Engine:
@model System.Data.DataSet
@using System.Data;
<h2>Report</h2>
<table>
<thead>
<tr>
@foreach (DataColumn col in Model.Tables[0].Columns)
{
<th>@col.ColumnName</th>
}
</tr>
</thead>
<tbody>
@foreach (DataRow row in Model.Tables[0].Rows)
{
<tr>
@foreach (DataColumn col in Model.Tables[0].Columns)
{
<td>@row[col.ColumnName]</td>
}
</tr>
}
</tbody>
</table>
ASPX Engine:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<System.Data.DataSet>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<h2><%: ViewData["Message"] %></h2>
<table border="1">
<thead>
<tr>
<%foreach (System.Data.DataColumn col in Model.Tables[0].Columns) { %>
<th><%=col.Caption %></th>
<%} %>
</tr>
</thead>
<tbody>
<%
foreach (System.Data.DataRow row in Model.Tables[0].Rows)
{ %>
<tr>
<%
foreach (var cell in row.ItemArray) {%>
<td><%=cell.ToString() %></td>
<%} %>
</tr>
<%} %>
</tbody>
</table>
</body>
</html>
Through ViewData[""]:
In Controller:
public ActionResult Index()
{
ViewData["Message"] = "Welcome to Nice One Code";
DataSet ds = new DataSet();
DataTable dt = new DataTable("NiceTable");
dt.Columns.Add(new DataColumn("Col1", typeof(string)));
dt.Columns.Add(new DataColumn("Col2", typeof(string)));
dt.Columns.Add(new DataColumn("Col3", typeof(string)));
for (int i = 0; i < 3; i++)
{
DataRow row = dt.NewRow();
row["Col1"] = "col 1, row " + i;
row["Col2"] = "col 2, row " + i;
row["Col3"] = "col 3, row " + i;
dt.Rows.Add(row);
}
ds.Tables.Add(dt);//Better way get dataset from model class
ViewData["ds"]=ds;
return View();
}
In View:
RAZOR Engine:
@using System.Data;
@{var ds=(System.Data.DataSet)ViewData["ds"];}
<h2>Report</h2>
<table>
<thead>
<tr>
@foreach (DataColumn col in ds.Tables[0].Columns)
{
<th>@col.ColumnName</th>
}
</tr>
</thead>
<tbody>
@foreach (DataRow row in ds.Tables[0].Rows)
{
<tr>
@foreach (DataColumn col in ds.Tables[0].Columns)
{
<td>@row[col.ColumnName]</td>
}
</tr>
}
</tbody>
</table>