Background
In earlier ASP.NET it was very straightforward and easy to display multiple tables data using DataSet but in ASP.NET MVC we need to mostly work with generic lists. Also in many forum posts and in my event speaking people asking me how to display multiple tables data in single view from database in ASP.NET MVC . So considering the preceding requirement I have decided to write this article .Now let's learn step by step, which helps beginners to learn how to display multiple tables data in single view from database in ASP.NET MVC.
In earlier ASP.NET it was very straightforward and easy to display multiple tables data using DataSet but in ASP.NET MVC we need to mostly work with generic lists. Also in many forum posts and in my event speaking people asking me how to display multiple tables data in single view from database in ASP.NET MVC . So considering the preceding requirement I have decided to write this article .Now let's learn step by step, which helps beginners to learn how to display multiple tables data in single view from database in ASP.NET MVC.
Scenario
Lets consider we have a requirement to display the master data in single view where Data coming from multiple tables . So in this scenario we need to create complex model class from multiple model classes.We have following two tables in Database from which we are going to display the data as in following screen shots.
So lets demonstrate preceding scenario by creating one simple ASP.NET MVC application
Lets consider we have a requirement to display the master data in single view where Data coming from multiple tables . So in this scenario we need to create complex model class from multiple model classes.We have following two tables in Database from which we are going to display the data as in following screen shots.
So lets demonstrate preceding scenario by creating one simple ASP.NET MVC application
Step 1: Create an MVC Application.
Now let us start with a step by step approach from the creation of a simple MVC application as in the following:
- "Start", then "All Programs" and select "Microsoft Visual Studio 2015".
- "File", then "New" and click "Project", then select "ASP.NET Web Application Template", then provide the Project a name as you wish and click OK. After clicking, the following window will appear:

Step 2 : Add The Reference of Dapper ORM into Project.
Now the next step is to add the reference of Dapper ORM into our created MVC Project. Here are the steps:
- Right click on Solution ,find Manage NuGet Package manager and click on it.
- After as shown into the image and type in search box "dapper".
- Select Dapper as shown into the image .
- Choose version of dapper library and click on install button.

Step 3: Create Model Class
Now let us create the model class file named CustomerModel.cs by right clicking on model folder as in the following screenshot:

Note:
It is not mandatory that Model class should be in Models folder, it is just for better readability; you can create this class anywhere in the solution explorer. This can be done by creating different folder names or without folder name or in a separate class library.
CustomerModel.cs class file code snippet:
- using System.Collections.Generic;
- namespace GetMultipleTableRecordsUsingDapper.Models
- {
- public class Customer
- {
- public string Name { get; set; }
- public string CustomerType { get; set; }
- public string Gender { get; set; }
- }
- public class Region
- {
- public string Country { get; set; }
- public string State { get; set; }
- public string City { get; set; }
- }
- public class MasterDetails
- {
- public List<Customer> CustPersonal { get; set; }
- public List <Region> CustRegions { get; set; }
- }
- }
Now let us add the MVC 5 controller as in the following screenshot:
After clicking on Add button it will show the window. Specify the Controller name as Customer with suffix Controller.
Note:
- Controller name must be having suffix as 'Controller' after specifying the name of controller.
Step 5 : Create Stored procedure.
Create the stored procedure to get the multiple table records from database as,
Create the stored procedure to get the multiple table records from database as,
- Create PROCEDURE GetMasterDetails
- as
- BEGIN
- SET NOCOUNT ON;
- Select Name,CustomerType,Gender from CustomerMaster
- Select Country,State,City from RegionMaster
- END
Step 6: Create Repository class.
Now create Repository folder and Add CustomerRepo.cs class for database related operations, Now create methods in CustomerRepo.cs to get the multiple table data from stored procedure with the help of Dapper ORM as in the following code snippet:
CustomerRepo.cs
- using Dapper;
- using GetMultipleTableRecordsUsingDapper.Models;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
- using System.Linq;
- namespace GetMultipleTableRecordsUsingDapper.Repository
- {
- public class CustomerRepo
- {
- //To Handle connection related activities
- SqlConnection con;
- private void connection()
- {
- string constr = ConfigurationManager.ConnectionStrings["SqlConn"].ToString();
- con = new SqlConnection(constr);
- }
- /// <summary>
- /// Get Multiple Table details
- /// </summary>
- /// <returns></returns>
- public IEnumerable<MasterDetails> GetMasterDetails()
- {
- connection();
- con.Open();
- var objDetails = SqlMapper.QueryMultiple(con, "GetMasterDetails",commandType: CommandType.StoredProcedure);
- MasterDetails ObjMaster = new MasterDetails();
- //Assigning each Multiple tables data to specific single model class
- ObjMaster.CustPersonal = objDetails.Read<Customer>().ToList();
- ObjMaster.CustRegions = objDetails.Read<Region>().ToList();
- List<MasterDetails> CustomerObj = new List<MasterDetails>();
- //Add list of records into MasterDetails list
- CustomerObj.Add(ObjMaster);
- con.Close();
- return CustomerObj;
- }
- }
- }
Note
- In the above code we are manually opening and closing connection, however you can directly pass the connection string to the dapper without opening it. Dapper will automatically handle it.
Now modify the default code in CustomerController.cs class file to bind multiple HTML tables in single view from strongly typed complex model class with list of records, After modifying code will look like as follows,
CustomerController.cs
CustomerController.cs
- using GetMultipleTableRecordsUsingDapper.Models;
- using GetMultipleTableRecordsUsingDapper.Repository;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web.Mvc;
- namespace GetMultipleTableRecordsUsingDapper.Controllers
- {
- public class CustomerController : Controller
- {
- // GET: Customer
- public ActionResult MasterDetail()
- {
- CustomerRepo objDet = new CustomerRepo();
- MasterDetails CustData = new MasterDetails();
- List<MasterDetails> MasterData = objDet.GetMasterDetails().ToList();
- CustData.CustPersonal = MasterData[0].CustPersonal;
- CustData.CustRegions = MasterData[0].CustRegions;
- return View(CustData);
- }
- }
- }




William OniszkoPosted Sep 29, 2021, 1:59 PM
Can you fix the table side-by-side instead of stacking them?
Former memberPosted Nov 30, 2018, 7:26 AM
@vithal Wadje Can you plz help me in getting values based on logged in user??
Lalit SinghPosted Mar 13, 2018, 10:19 AM
Where is download link?
Hector CPosted Oct 2, 2017, 10:29 PM
Step 5 how to do that? I'm new in MVC please help. Where should I create the procedure?
Song ZhangPosted May 12, 2017, 10:56 AM
Where is the zip application file?
Vithal WadjePosted Sep 14, 2016, 8:26 AM
Read sentence what I have written "improve it depending on your skills.."
Vithal WadjePosted Apr 24, 2016, 1:08 AM
thanks
Humayun Kabir MamunPosted Apr 24, 2016, 12:58 AM
Nice...
Vithal WadjePosted Apr 22, 2016, 11:26 PM
thanks
Bhavik PatelPosted Apr 22, 2016, 11:00 PM
great share
Vithal WadjePosted Apr 20, 2016, 11:17 PM
Thanks
Vignesh ManiPosted Apr 20, 2016, 4:09 PM
Nice