Working With Multiple Tables in MVC Using LINQ to SQL

Database structure

Create two tables in the database with the name department and employee.
 
The following is the create table code for the department table:
  1. CREATE TABLE [dbo].[department]  
  2. (  
  3.    [departmentid] [intNOT NULL,  
  4.    [name] [nvarchar](50) NULL,  
  5. )  
The following is the create table code for the employee table:
  1. CREATE TABLE [dbo].[employee]  
  2. (  
  3.    [employeid] [intNOT NULL,  
  4.    [name] [nvarchar](50) NULL,  
  5.    [gender] [nvarchar](50) NULL,  
  6.    [city] [nvarchar](50) NULL,  
  7.    [dpmtid] [intNULL,  
  8. )  
Create MVC Application

Step 1

Go to File => New => Project.
 
Step 2

Choose "ASP.NET MVC 4 Web Application" from the list, then provide the application name as "MVCtestlinq" and set the path in the location input where you want to create the application.
 
Step 3

Now choose the Project Template "Empty".
 
Adding a LINQ to SQL Class

Step 1

Right-click on the project and select "Add new item". Then, select Data from the templates.
 
Step 2

Choose "LINQ to SQL classes" from the list and provide a name. Now, after clicking on Add, you can see the .dbml file in the project.
 
Step 3

Drag the department and employee table from the database in the Server Explorer.


|

Create Model Class

The MVC model contains all the application logic validation, business logic and data access logic. We can create a department class under the Model Folder.
  1. sing System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace Mvctestlinq.Models  
  7. {  
  8.     public class depart  
  9.     {  
  10.         public List<department> depatmentss { setget; }  
  11.         public List<employee> empss { setget; }  
  12.     }  
  13. }  
Create a department controller
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using Mvctestlinq.Models;  
  7.   
  8. namespace Mvctestlinq.Controllers  
  9. {  
  10.     public class departmentController : Controller  
  11.     {  
  12.         //  
  13.         // GET: /department/  
  14.           
  15.         public ActionResult Index()  
  16.         {  
  17.             DataClasses1DataContext dpr = new DataClasses1DataContext();  
  18.           //  depart db = new depart();  
  19.             List<department> dps= dpr.departments.ToList();  
  20.             return View(dps);  
  21.         }  
  22.     }  
  23. }  
Create a view to show the department name Name

Right-click on the Index Actionresult method and select Add view. After selecting add view, a dialog box will open. The view name is index by default. Now, select your model class and click on the OK button.
 
Let's see each view with code.
  1. @model  IEnumerable< Mvctestlinq.department>  
  2.   
  3. @{  
  4.     ViewBag.Title = " Deaprtment name ";  
  5. }  
  6.   
  7. <h2>Index</h2>  
  8. @{  
  9.     ViewBag.Title = "Department";  
  10. }  
  11.   
  12. <h2>Department</h2>  
  13.   
  14. <div>  
  15.     <ul>  
  16.         @foreach (var item in @Model)  
  17.         {  
  18.             <li>  
  19.   
  20.                 @Html.ActionLink(item.name,"index","emp",new {departmentid=item.departmentid}, null )  
  21.             </li>  
  22.         }  
  23.     </ul>  
  24. </div>  
The following is the output of the preceding code.

If we will click on any name of department list, the page redirects to the emp controller and calls the index method. The index method shows the total number of employees in the department with their names.
 
Create controller class for emp
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using Mvctestlinq.Models;  
  7.   
  8. namespace Mvctestlinq.Controllers  
  9. {  
  10.     public class empController : Controller  
  11.     {  
  12.         //  
  13.         // GET: /emp/  
  14.         DataClasses1DataContext db = new DataClasses1DataContext();  
  15.         public ActionResult Index(int departmentid)  
  16.         {  
  17.             List<employee> employed = db.employees.Where(emps => emps.dpmtid == departmentid).ToList();  
  18.   
  19.             return View(employed); 
  20.         }  
  21.         public ActionResult details(int id)  
  22.         {  
  23.             DataClasses1DataContext db = new DataClasses1DataContext();  
  24.   
  25.             empcontext empss = new empcontext();  
  26.             empss.empt = db.employees.ToList();  
  27.             var value = empss.empt.Single(d => d.employeid == id);  
  28.             return View(value);  
  29.         }  
  30.     }  
Create a view to show the Employe list

Right-click on the Index Actionresult method and select Add view. After selecting add view, a dialog box will open. The view name is index by default. Now, select your model class and click on the OK button.
 
Let's see each view with code.
  1. @model IEnumerable< Mvctestlinq.employee>  
  2.   
  3. @{  
  4.     ViewBag.Title = "Employe list";  
  5. }  
  6.   
  7. <h2>Employe List</h2>  
  8.   
  9. <div>  
  10.   
  11. <ul>  
  12.         @foreach (var employe in @Model)  
  13.         {  
  14.               
  15.             <li>  @Html.ActionLink(employe.name, "details", new {idemploye.employeid  })</li>  
  16.          }       
  17. </ul>  
  18. </div>  
The following is the output of the preceding code.

If we will click any name from the employee list, the page will redirect the emp controller and call the details method. The details method shows the details of employee:
  1. @model  Mvctestlinq.employee  
  2.   
  3. @{  
  4.     ViewBag.Title = "details";  
  5. }  
  6.   
  7. <h2>details</h2>  
  8.   
  9. <div>  
  10.     <table>  
  11.         <tr>  
  12.             <td>  
  13.                 <b>employe id</b>  
  14.             </td>  
  15.             <td>  
  16.                 @Model.employeid  
  17.             </td>  
  18.         </tr>  
  19.         <tr>  
  20.             <td>  
  21.                 <b>Name</b>  
  22.             </td>  
  23.             <td>  
  24.                 @Model.name  
  25.             </td>  
  26.         </tr>  
  27.         <tr>  
  28.             <td>  
  29.                 <b>Gender</b>  
  30.             </td>  
  31.             <td>  
  32.                 @Model.gender  
  33.             </td>  
  34.         </tr>  
  35.         <tr>  
  36.             <td>  
  37.                 <b>city</b>  
  38.             </td>  
  39.             <td>  
  40.                 @Model.city  
  41.             </td>  
  42.         </tr>  
  43.          
  44.     </table>  
  45.   
  46.     @Html.ActionLink("back to list", "Index", new { departmentid = @Model.dpmtid })  
  47. </div>  
The following is the output of the preceding code.



Summary 

In this article, we learned how to use multiple tables in MVC using LINQ to SQL.

Up Next
    Ebook Download
    View all
    Learn
    View all