Created
November 23, 2015 11:35
-
-
Save Tesla9527/3984542a133582b2db55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
----------------------------- | |
Below is code in EmployeeController.cs | |
----------------------------- | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
using WebApplication2.Models; | |
namespace WebApplication2.Controllers | |
{ | |
public class EmployeeController : Controller | |
{ | |
public ActionResult Index(int departmentId) | |
{ | |
EmployeeContext employeeContext = new EmployeeContext(); | |
List<Employee> employees = employeeContext.Employees.Where(emp => emp.DepartmentId == departmentId).ToList(); | |
return View(employees); | |
} | |
public ActionResult Details(int id) | |
{ | |
EmployeeContext employeeContext = new EmployeeContext(); | |
Employee employee = employeeContext.Employees.Single(emp => emp.EmployeeId == id); | |
return View(employee); | |
} | |
} | |
} | |
----------------------------- | |
Below is code in DepartmentController.cs | |
----------------------------- | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
using WebApplication2.Models; | |
namespace WebApplication2.Controllers | |
{ | |
public class DepartmentController : Controller | |
{ | |
public ActionResult Index() | |
{ | |
EmployeeContext employeeContext = new EmployeeContext(); | |
List<Department> departments = employeeContext.Departments.ToList(); | |
return View(departments); | |
} | |
} | |
} | |
----------------------------- | |
Below is code in Employee.cs | |
----------------------------- | |
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel.DataAnnotations.Schema; | |
using System.Linq; | |
using System.Web; | |
namespace WebApplication2.Models | |
{ | |
[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; } | |
} | |
} | |
----------------------------- | |
Below is code in Department.cs | |
----------------------------- | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.ComponentModel.DataAnnotations.Schema; | |
namespace WebApplication2.Models | |
{ | |
[Table("tblDepartment")] | |
public class Department | |
{ | |
public int ID { get; set; } | |
public string Name { get; set; } | |
public List<Employee> Employees { get; set; } | |
} | |
} | |
----------------------------- | |
Below is code in EmployeeContext.cs | |
----------------------------- | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Data.Entity; | |
namespace WebApplication2.Models | |
{ | |
public class EmployeeContext : DbContext | |
{ | |
public DbSet<Employee> Employees { get; set; } | |
public DbSet<Department> Departments { get; set; } | |
} | |
} | |
----------------------------- | |
Below is code in index.cshtml under folder Department | |
----------------------------- | |
@using WebApplication2.Models; | |
@model IEnumerable<Department> | |
@{ | |
ViewBag.Title = "Department list"; | |
} | |
<div style="font-family:Arial"> | |
<h2>Department list</h2> | |
<ul> | |
@foreach (Department department in @Model) | |
{ | |
<li> | |
@Html.ActionLink(department.Name, "Index", "Employee", new { departmentId = department.ID },null) | |
</li> | |
} | |
</ul> | |
</div> | |
----------------------------- | |
Below is code in Details.cshtml under folder Employee | |
----------------------------- | |
@model WebApplication2.Models.Employee | |
@{ | |
ViewBag.Title = "Employee Details"; | |
} | |
<h2>Employee Details</h2> | |
<table style="font-family:Arial"> | |
<tr> | |
<td> | |
<b>Employee Id:</b> | |
</td> | |
<td> | |
@Model.EmployeeId | |
</td> | |
</tr> | |
<tr> | |
<td> | |
<b>Name:</b> | |
</td> | |
<td> | |
@Model.Name | |
</td> | |
</tr> | |
<tr> | |
<td> | |
<b>Gender:</b> | |
</td> | |
<td> | |
@Model.Gender | |
</td> | |
</tr> | |
<tr> | |
<td> | |
<b>City:</b> | |
</td> | |
<td> | |
@Model.City | |
</td> | |
</tr> | |
</table> | |
@Html.ActionLink("Back to employee list","Index",new { departmentId = Model.DepartmentId}) | |
----------------------------- | |
Below is code in index.cshtml under folder Employee | |
----------------------------- | |
@model IEnumerable<WebApplication2.Models.Employee> | |
@using WebApplication2.Models; | |
@{ | |
ViewBag.Title = "Employee list"; | |
} | |
<div> | |
<h2>Employee list</h2> | |
<ul> | |
@foreach (Employee employee in Model) | |
{ | |
<li> | |
@Html.ActionLink(employee.Name, "Details", new { id = employee.EmployeeId }) | |
</li> | |
} | |
</ul> | |
</div> | |
@Html.ActionLink("Back to department list", "Index","Department") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment