Project

My Table
Figure 1: Project Table

The following is the script of the table:

  1. CREATE TABLE [dbo].[Project](
  2. [ProjectID] [int] IDENTITY(1,1) NOT NULL,
  3. [ProjectName] [varchar](50) NULL,
  4. [ProjectLeader] [varchar](50) NULL,
  5. CONSTRAINT [PK_Project] PRIMARY KEY CLUSTERED
  6. (
  7. [ProjectID] ASC
  8. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  9. ) ON [PRIMARY]
  10. GO
  11. SET ANSI_PADDING OFF
  12. GO
And the second table is Employee:

table is Employee
Figure 2: Employee Table

The script of the Employee table is:
  1. CREATE TABLE [dbo].[Employee](
  2. [ID] [int] IDENTITY(1,1) NOT NULL,
  3. [Name] [varchar](50) NULL,
  4. [Email] [varchar](500) NULL,
  5. [Country] [varchar](50) NULL,
  6. [ProjectID] [int] NULL,
  7. CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
  8. (
  9. [ID] ASC
  10. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  11. ) ON [PRIMARY]
  12. GO
  13. SET ANSI_PADDING OFF
  14. GO
Now create a new Visual Studio Project.

new Visual Studio Project
Figure 3: Creating a new Visual Studio project

internet application
Figure 4: Visual Studio Project

Now right-click on the Project Solution Explorer and Add New ADO.NET Entity Data Model as in the following:

add project
Figure 5: Add New ADO.NET Entity Data Model

click next
Figure 6: Choose Model Contents

clock ok
Figure 7: Connection Properties

Employee project
Figure 8: Choose your Data Connection

finish
Figure 9: Choose Database objects and settings

design
Figure 10: Tables are created

Now see the data in the tables, Project and Employee:

see Data
Figure 11: Added some data

Employee Tables
Figure 12: Employee Table

Now right-click on the Model folder inside your project's Solution Explorer and Add New Item. Now add a new class and name it EmpProject.cs.

Class
Figure 13: New class created

Write the following code snippet.

code
Figure 14: Code written
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. namespace ShowingNestedDataDisplayInMVC4.Models
  6. {
  7. public class EmpProject
  8. {
  9. public Project project { get; set; }
  10. public List<Employee> projectEmployee { get; set; }
  11. }
  12. }
Now right-click on Controller and Add New Controller.

new controller
Figure 15: Add New Controller

project controller
Figure 16: Added Project Controller

Write the following code in the project's Controller:


Figure 17: Code added.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using ShowingNestedDataDisplayInMVC4.Models;
  7. namespace ShowingNestedDataDisplayInMVC4.Controllers
  8. {
  9. public class ProjectController : Controller
  10. {
  11. //
  12. // GET: /Project/
  13. public ActionResult Index()
  14. {
  15. List<EmpProject> prjEMPReport = new List<EmpProject>();
  16. // here MyDatabaseEntities is our data context
  17. using (EmployeeProjectEntities dc = new EmployeeProjectEntities())
  18. {
  19. var prjRecords = dc.Project.OrderByDescending(a => a.ProjectID);
  20. foreach (var i in prjRecords)
  21. {
  22. var empPrjDTL = dc.Employee.Where(a => a.ProjectID == (i.ProjectID)).ToList();
  23. prjEMPReport.Add(new EmpProject { project = i, projectEmployee = empPrjDTL });
  24. }
  25. }
  26. return View(prjEMPReport);
  27. }
  28. }
  29. }
Now right-click on the Index Action method and select Add View.

Add View
Figure 18: Add View

In Index.cshtm write the following code:
  1. @model IEnumerable<ShowingNestedDataDisplayInMVC4.Models.EmpProject>
  2. @{
  3. ViewBag.Title = "Displaying Data In Nested Grid View In MVC4";
  4. WebGrid grid = new WebGrid(source: Model, canSort: false);
  5. }
  6. <style>
  7. #gridT, #gridT tr {
  8. border: 1px solid #0D857B;
  9. }
  10. #nestedT, #nestedT tr {
  11. border: 1px solid #f3f3f3;
  12. }
  13. #nestedT {
  14. margin: 0px 0px 0px 10px;
  15. padding: 5px;
  16. width: 95%;
  17. }
  18. #nestedT th {
  19. font-size: 14px;
  20. font-weight: bold;
  21. background-color: green;
  22. }
  23. .recordHoverEffect {
  24. cursor: pointer;
  25. }
  26. .recordHoverEffect:hover {
  27. background-color: rgb(248, 242, 242);
  28. }
  29. .expandRecordRecord {
  30. background-image: url(/Images/ShowHideImage.png);
  31. background-position-x: -22px;
  32. background-repeat: no-repeat;
  33. }
  34. .collapseRecordRecord {
  35. background-image: url(/Images/ShowHideImage.png);
  36. background-position-x: -2px;
  37. background-repeat: no-repeat;
  38. }
  39. </style>
  40. @section Scripts{
  41. <script>
  42. $(document).ready(function () {
  43. var size = $("#main #gridT > thead > tr >th").size();
  44. $("#main #gridT > thead > tr >th").last().remove();
  45. $("#main #gridT > thead > tr").prepend("<th></th>");
  46. $("#main #gridT > tbody > tr").each(function (i, el) {
  47. $(this).prepend(
  48. $("<td></td>")
  49. .addClass("expandRecord")
  50. .addClass("recordHoverEffect")
  51. .attr('title', "Show Hide Details")
  52. );
  53. var table = $("table", this).parent().html();
  54. $(this).after("<tr><td></td><td style='padding:5px; margin:0px;' colspan='" + (size - 1) + "'>" + table + "</td></tr>");
  55. $("table", this).parent().remove();
  56. $(".recordHoverEffect", this).live("click", function () {
  57. $(this).parent().closest("tr").next().slideToggle(100);
  58. $(this).toggleClass("expandRecord collapseRecord");
  59. });
  60. });
  61. $("#main #gridT > tbody > tr td.expandRecord").each(function (i, el) {
  62. $(this).toggleClass("expandRecord collapseRecord");
  63. $(this).parent().closest("tr").next().slideToggle(100);
  64. });
  65. });
  66. </script>
  67. }
  68. <div id="main" style="padding: 25px; background-color: yellow;">
  69. @grid.GetHtml(
  70. htmlAttributes: new { id = "gridT", width = "900px", style = "background-color:skyblue; color:blue; font-size:14pt; font-family:TimesNewRoman;" },
  71. columns: grid.Columns(
  72. grid.Column("project.ProjectID", "Project ID"),
  73. grid.Column("project.ProjectName", "Project Name"),
  74. grid.Column("project.ProjectLeader", "Project Leader"),
  75. grid.Column(format: (item) =>
  76. {
  77. WebGrid subGrid = new WebGrid(source: item.projectEmployee);
  78. return subGrid.GetHtml(
  79. htmlAttributes: new { id = "nestedT", style = "background-color:red; color:white; font-size:12pt; font-family:verdana;" },
  80. columns: subGrid.Columns(
  81. subGrid.Column("ID", " Employee ID"),
  82. subGrid.Column("Name", "Name"),
  83. subGrid.Column("Email", "Email"),
  84. subGrid.Column("Country", "Country")
  85. )
  86. );
  87. })
  88. )
  89. )
  90. </div>
You can set your view as start up, for this open Route.config inside App_Start Folder and make the following settings:

setting
Figure 19: RouteConfig.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Web.Routing;
  7. namespace ShowingNestedDataDisplayInMVC4
  8. {
  9. public class RouteConfig
  10. {
  11. public static void RegisterRoutes(RouteCollection routes)
  12. {
  13. routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  14. routes.MapRoute(
  15. name: "Default",
  16. url: "{controller}/{action}/{id}",
  17. defaults: new { controller = "Project", action = "Index", id = UrlParameter.Optional }
  18. );
  19. }
  20. }
  21. }
Now run your application:

output
Figure 20: Output

Now go to Project ID to expand the details of this project as in the following:

show hide detail
Figure 21: Expand the details

emp email
Figure 22: After expanding the details

project id
Figure 23: After expanding the details of Project ID 2

employee
Figure 24: Details for Project ID 3

product

Figure 25: Details for Project ID 1, 2 and 3