Hi nauna,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
Model
public class OrderDetails
{
public string CustomerId { get; set; }
public string Name { get; set; }
public string Country { get; set; }
public List<Order> Orders { get; set; }
}
public class Order
{
public string OrderId { get; set; }
public string Freight { get; set; }
public string ShipCountry { get; set; }
}
Namespaces
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
return View(GetCustomerOrders());
}
string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
public List<OrderDetails> GetCustomerOrders()
{
List<OrderDetails> customers = new List<OrderDetails>();
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
con.Open();
cmd.CommandText = "SELECT TOP 2 CustomerId,ContactName,Country FROM Customers ";
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
OrderDetails customer = new OrderDetails
{
CustomerId = rdr["CustomerId"].ToString(),
Name = rdr["ContactName"].ToString(),
Country = rdr["Country"].ToString(),
Orders = Orders(rdr["CustomerId"].ToString())
};
customers.Add(customer);
}
con.Close();
}
}
return customers;
}
public List<Order> Orders(string customerId)
{
List<Order> orders = new List<Order>();
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
con.Open();
cmd.CommandText = "SELECT TOP 5 OrderId,Freight,ShipCountry FROM Orders WHERE CustomerId = @CustomerId";
cmd.Parameters.AddWithValue("@CustomerId", customerId);
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Order order = new Order
{
OrderId = rdr["OrderId"].ToString(),
Freight = rdr["Freight"].ToString(),
ShipCountry = rdr["ShipCountry"].ToString(),
};
orders.Add(order);
}
con.Close();
}
}
return orders;
}
}
View
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<_Nested_Data_Bind_MVC.Models.OrderDetails>>" %>
<!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>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Country</th>
<th>Order</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td><%: item.CustomerId %></td>
<td><%: item.Name %></td>
<td><%: item.Country %></td>
<td>
<table>
<tr>
<td>OrderID</td>
<td>Freight</td>
<td>ShipCountry</td>
</tr>
<%foreach (var order in item.Orders) {%>
<tr>
<td><%: order.OrderId %></td>
<td><%: order.Freight %></td>
<td><%: order.ShipCountry %></td>
</tr>
<%} %>
</table>
</td>
</tr>
<% } %>
</table>
</body>
</html>
Screenshot