Premium Only Content
Part 10 Working with multiple tables in mvc
In this video we will discuss working with 2 related tables in MVC
1. tblDepartment
2. tblEmployee
This is what we want to achieve
1. Display all the departments from tblDepartments table. The Department names should be rendered as hyperlinks.
2. On clicking the department name link, all the employees in the department should be displayed. The employee names should be rendered as hyperlinks.
3. On clicking the employee name link, the full details of the employee should be displayed.
4. A link should also be provided on the employee full details page to navigate back to Employee list page. Along the same lines, a link should also be provided on the employee list page to navigate back to Departments list page.
Implementing Departments List:
Step 1: Right click on the "Models" folder and add a class file with name=Department.cs. Copy and paste the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
namespace MVCDemo.Models
{
[Table("tblDepartment")]
public class Department
{
public int ID { get; set; }
public string Name { get; set; }
public List[Employee] Employees { get; set; }
}
}
Step 2: Add "Departments" property to "EmployeeContext" class that is present in "EmployeeContext.cs" file in "Models" folder.
public class EmployeeContext : DbContext
{
public DbSet[Department] Departments { get; set; }
public DbSet[Employee] Employees { get; set; }
}
Step 3: Right click on the "Controllers" folder and add a Controller, with name=DepartmentController. Copy and paste the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCDemo.Models;
namespace MVCDemo.Controllers
{
public class DepartmentController : Controller
{
public ActionResult Index()
{
EmployeeContext employeeContext = new EmployeeContext();
List[Department] departments = employeeContext.Departments.ToList();
return View(departments);
}
}
}
Step 4: Right click on the Index() action method in DepartmentController class and select "Add View" from the context menu. Set
View name = Index
View engine = Razor
Select "Create Strongly-typed view checkbox
Select Department class, from "Model class" dropdownlist
Click "Add" button
Copy and paste the following code in Index.cshtml view file in Department folder
@using MVCDemo.Models;
@model IEnumerable[Department]
[div style="font-family:Arial"]
@{
ViewBag.Title = "Departments List";
}
[h2]Departments List[/h2]
[ul]
@foreach (Department department in @Model)
{
[li]@Html.ActionLink(department.Name, "Index", "Employee",
new { departmentId = department.ID }, null)[/li]
}
[/ul]
[/div]
Changes to Employee List and Detail pages
Add "DepartmentId" propertu to "Employee" model class that is present in Employee.cs file in "Models" folder.
[Table("tblEmployee")]
public class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
public int DepartmentId { get; set; }
}
Add "departmentId" parameter to Index() action method in "EmployeeController" class that is present in "EmployeeController.cs" file in "Controllers" folder. Use the "departmentId" parameter to filter the list of employees as shown below.
public ActionResult Index(int departmentId)
{
EmployeeContext employeeContext = new EmployeeContext();
List[Employee] employees = employeeContext.Employees.Where(emp =] emp.DepartmentId == departmentId).ToList();
return View(employees);
}
Copy and paste the following line in "Index.cshtml" that is present in "Employee" folder in "Views" folder. With this change we are able to generate an action link to redirect the user to a different controller action method.
@Html.ActionLink("Back to Department List", "Index", "Department")
-
1:30:30
Game On!
14 hours ago $5.48 earnedTop 5 things you need to know for Sports Christmas!
31K3 -
1:58:10
Robert Gouveia
23 hours agoMatt Gaetz REJECTS Report, Sues Committee; Luigi Fan Club Arrives; Biden Commutes; Festivus Waste
261K202 -
1:31:40
Adam Does Movies
23 hours ago $14.13 earnedThe Best & Worst Christmas Movies! - LIVE!
92.6K8 -
58:10
Kimberly Guilfoyle
1 day agoAmerica is Back & The Future is Bright: A Year in Review | Ep. 183
183K70 -
3:03:27
vivafrei
1 day agoEp. 242: Barnes is BACK AGAIN! Trump, Fani, J6, RFK, Chip Roy, USS Liberty AND MORE! Viva & Barnes
256K247 -
2:05:48
2 MIKES LIVE
6 hours agoTHE MIKE SCHWARTZ SHOW with DR. MICHAEL J SCHWARTZ 12-24-2024
32.1K3 -
1:14:17
MTNTOUGH Fitness Lab
1 day agoNavy SEAL Dom Raso: The Cold, Hard Truth About Modern Brotherhood | MTNPOD #96
25.5K4 -
43:42
Dad Dojo Podcast
22 hours ago $0.69 earnedEP14: Every Girl Dad's Biggest Fear and How To Prevent It
17.7K -
55:06
Bek Lover Podcast
15 hours agoWill Trump Pull Off A Miracle? Other Strange News Podcast...
15.1K17 -
55:53
PMG
1 day ago $0.52 earned"Hannah Faulkner and Courtney Reed | BEHIND THE LENS OF A TRUMP PHOTOGRAPHER"
12.4K