Introduction
This article will explain to you how you can display multiple tables' record in a single view using ViewModel in ASP.Net MVC. For this article, you should have basic understanding of the following,
- Bootstrap 4
- MVC
- Entity Framework
- View Model
- SQL Server
Step 1
Open your favourite SQL server database any version. It doesn’t really matter. Create tables as shown below.
- CREATE TABLE [dbo].[Employee](
- [Id] [int] IDENTITY(1,1) NOT NULL,
- [Name] [nvarchar](50) NULL,
- [Gender] [char](10) NULL,
- [Age] [int] NULL,
- [Position] [nvarchar](50) NULL,
- [Office] [nvarchar](50) NULL,
- [Salary] [int] NULL,
- CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
- (
- [Id] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
- CREATE TABLE [dbo].[Department](
- [Id] [int] IDENTITY(1,1) NOT NULL,
- [DepartmentName] [nvarchar](50) NULL,
- CONSTRAINT [PK_Department] PRIMARY KEY CLUSTERED
- (
- [Id] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
- CREATE TABLE [dbo].[Incentive](
- [Id] [int] IDENTITY(1,1) NOT NULL,
- [IncentiveAmount] [int] NULL,
- CONSTRAINT [PK_Incentive] PRIMARY KEY CLUSTERED
- (
- [Id] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
Step 2
Now open your favorite Visual Studio 2017 or any version you wish to.

Step 3
Create an empty project in Visual Studio, and give an appropriate name. Check MVC checkbox and click on OK.

Step 4
Right-click on the Models folder and add a database model. Add Entity Framework now. For that, right-click on Models folder, select Add, then select New Item.

You will get a window; from there, select Data from the left panel and choose ADO.NET Entity Data Model, give it the name MyModel (this name is not mandatory, you can give any name) and click "Add"

After you click on "Add a window", the wizard will open. Choose EF Designer from the database and click "Next".

After clicking on "Next", a window will appear. Choose New Connection. Another window will appear. Add your server name - if it is local, then enter a dot (.). Choose your database and click "OK".

The connection will be added. If you wish, save the connection name as you want. You can change the name of your connection below. It will save the connection in the web config. Now, click "Next".

After clicking on NEXT, another window will appear. Choose the database table name as shown in the below screenshot and click "Finish".

Entity Framework gets added and the respective class gets generated under the Models folder.

Step 5
Right-click on Controllers folder and add a controller.

A window will appear. Choose MVC5 Controller-Empty and click "Add".

After clicking on "Add", another window will appear with DefaultController. Change the name to HomeController and click "Add". The HomeController will be added under the Controllers folder. Don’t change the Controller suffix for all controllers, change only the highlight, and instead of Default, just change Home.

Step 6
Right click on project “Add Folder” name it ViewModel. Now right click on this folder and “Add Class” with name EmployeeViewModel.
- using MvcMultipleTable_Demo.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace MvcMultipleTable_Demo.ViewModel
- {
- public class EmployeeViewModel
- {
- public IEnumerable<Employee> Employees { get; set; }
- public IEnumerable<Department> Departments { get; set; }
- public IEnumerable<Incentive> Incentives { get; set; }
- }
- }
Controller Class Code
- using MvcMultipleTable_Demo.Models;
- using MvcMultipleTable_Demo.ViewModel;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace MvcMultipleTable_Demo.Controllers
- {
- public class HomeController : Controller
- {
- private readonly EmployeeContext db = new EmployeeContext();
- public ActionResult Index()
- {
- var tables = new EmployeeViewModel
- {
- Employees=db.Employees.ToList(),
- Departments=db.Departments.ToList(),
- Incentives=db.Incentives.ToList()
- };
- return View(tables);
- }
- }
- }
Step 7
Right click on Index action method in controller class “Add View”


Index View Code
- @model MvcMultipleTable_Demo.ViewModel.EmployeeViewModel
- @{
- ViewBag.Title = "Index";
- }
- <ul class="nav nav-pills">
- <li class="nav-item ">
- <a class="nav-link active rounded-0" data-toggle="pill" href="#home">Employee</a>
- </li>
- <li class="nav-item">
- <a class="nav-link rounded-0" data-toggle="pill" href="#menu1">Department</a>
- </li>
- <li class="nav-item">
- <a class="nav-link rounded-0" data-toggle="pill" href="#menu2">Incentive</a>
- </li>
- </ul>
- <div class="tab-content">
- <div class="tab-pane container active p-0" id="home">
- <h5 class="text-uppercase p-2 text-center">List of Employee</h5>
- <table class="table table-bordered table-striped">
- <thead class="thead-dark text-white">
- <tr>
- <th>@Html.DisplayNameFor(m => Model.Employees.FirstOrDefault().Name)</th>
- <th>@Html.DisplayNameFor(m => Model.Employees.FirstOrDefault().Gender)</th>
- <th>@Html.DisplayNameFor(m => Model.Employees.FirstOrDefault().Age)</th>
- <th>@Html.DisplayNameFor(m => Model.Employees.FirstOrDefault().Position)</th>
- <th>@Html.DisplayNameFor(m => Model.Employees.FirstOrDefault().Office)</th>
- <th>@Html.DisplayNameFor(m => Model.Employees.FirstOrDefault().Salary)</th>
- </tr>
- </thead>
- <tbody>
- @foreach (var employee in Model.Employees)
- {
- <tr>
- <td>@employee.Name</td>
- <td>@employee.Gender</td>
- <td>@employee.Age</td>
- <td>@employee.Position</td>
- <td>@employee.Office</td>
- <td>@employee.Salary</td>
- </tr>
- }
- </tbody>
- </table>
- </div>
- <div class="tab-pane container fade p-0" id="menu1">
- <h5 class="text-uppercase p-2 text-center">List of Department</h5>
- <table class="table table-bordered table-striped">
- <thead class="thead-dark text-white">
- <tr>
- <th>@Html.DisplayNameFor(m => Model.Departments.FirstOrDefault().Id)</th>
- <th>@Html.DisplayNameFor(m => Model.Departments.FirstOrDefault().DepartmentName)</th>
- </tr>
- </thead>
- <tbody>
- @foreach (var employee in Model.Departments)
- {
- <tr>
- <td>@employee.Id</td>
- <td>@employee.DepartmentName</td>
- </tr>
- }
- </tbody>
- </table>
- </div>
- <div class="tab-pane container fade p-0" id="menu2">
- <h5 class="text-uppercase p-2 text-center">List of Incentive</h5>
- <table class="table table-bordered table-striped">
- <thead class="thead-dark text-white">
- <tr>
- <th>@Html.DisplayNameFor(m => Model.Incentives.FirstOrDefault().Id)</th>
- <th>@Html.DisplayNameFor(m => Model.Incentives.FirstOrDefault().IncentiveAmount)</th>
- </tr>
- </thead>
- <tbody>
- @foreach (var employee in Model.Incentives)
- {
- <tr>
- <td>@employee.Id</td>
- <td>@employee.IncentiveAmount</td>
- </tr>
- }
- </tbody>
- </table>
- </div>
- </div>
Step 8
Install latest version of bootstrap and JQuery from NuGet package manager under tools in Visual Studio.
Step 9
Add the following styles and script link in layout view,
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>@ViewBag.Title - My ASP.NET Application</title>
- <link href="~/Content/Site.css" rel="stylesheet" type="text/css" />
- <link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" />
- <script src="~/Scripts/modernizr-2.6.2.js"></script>
- </head>
- <body>
- <div class="container body-content">
- @RenderBody()
- </div>
- <script src="~/Scripts/jquery-3.4.1.min.js"></script>
- <script src="~/Scripts/bootstrap.min.js"></script>
- <script src="~/Scripts/popper.min.js"></script>
- </body>
- </html>
Step 10
Build your project and run by pressing Ctrl+F5




fatemeh qanbariPosted Sep 6, 2020, 3:01 AM
I want to post this parameters to my controller from my form How can I do?
Paul EvansPosted Aug 18, 2020, 5:06 AM
This is a great article Thanks very much. Simple to follow
ZIAD ABUQASEMPosted May 29, 2020, 10:05 AM
Assalam Alykum brother Farhan , I have a question , i need to add department name to the employee list , i tried but i cannot select <td>@employee.DepartmentName</td> in first employee list , how can you add it , thank you for your effort .
Sourav Kumar DasPosted Oct 14, 2019, 2:17 AM
Awesome Article.