Hinauna,
Instead of returning DataTable from Index action method return DataSet and pass the DataSet to the View.
Refer below article for passing DataSet to View.
Check this example. Now please take its reference and correct your code.
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
System.Data.DataSet ds = new System.Data.DataSet();
ds.Tables.Add(SelectFirstCategories());
ds.Tables.Add(SelectAllAds());
return View(ds);
}
private System.Data.DataTable SelectFirstCategories()
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.TableName = "Category";
dt.Columns.AddRange(new System.Data.DataColumn[2] {
new System.Data.DataColumn("Id", typeof(int)),
new System.Data.DataColumn("Name", typeof(string)) });
dt.Rows.Add(1, "Category 1");
dt.Rows.Add(2, "Category 2");
dt.Rows.Add(3, "Category 3");
dt.Rows.Add(4, "Category 4");
return dt;
}
private System.Data.DataTable SelectAllAds()
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.TableName = "Ads";
dt.Columns.AddRange(new System.Data.DataColumn[2] {
new System.Data.DataColumn("Id", typeof(int)),
new System.Data.DataColumn("Name", typeof(string)) });
dt.Rows.Add(1, "Ads 1");
dt.Rows.Add(2, "Ads 2");
dt.Rows.Add(3, "Ads 3");
dt.Rows.Add(4, "Ads 4");
return dt;
}
}
View
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<System.Data.DataSet>" %>
<!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>Index</title>
</head>
<body>
<h3>Categories</h3>
<table>
<tr>
<th>Id</th>
<th>Name</th>
</tr>
<%foreach (System.Data.DataRow row in Model.Tables[0].Rows) {%>
<tr>
<td><%=row["Id"]%></td>
<td><%=row["Name"]%></td>
</tr>
<%} %>
</table>
<hr />
<h3>Ads</h3>
<table>
<tr>
<th>Id</th>
<th>Name</th>
</tr>
<%foreach (System.Data.DataRow row in Model.Tables[1].Rows) {%>
<tr>
<td><%=row["Id"]%></td>
<td><%=row["Name"]%></td>
</tr>
<%} %>
</table>
</body>
</html>
Screenshot