This is what we want to achieve1. 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.

The screen shots of the above workflow is shown below for your reference

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;

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;

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")

Change the following line in "Details.cshtml" that is present in "Employee" folder in "Views" folder. CHANGE THIS LINE @Html.ActionLink("Back to List", "Index") TO @Html.ActionLink("Back to Employee List", "Index", new { departmentId = @Model.DepartmentId })

32 comments:

I have been waiting for your MVC series for a while. Thank You. You approach the subject from a whole different perspective.I enjoy you approach. Both educational and informative. Can I ask Venkat was there any reason not have created a BusinessContext class? Any special reason to add to the EmployeeContext.cs. Also you added in the Department class a List Employee property. Would that be a one to many relationship concept in like a database?

Help me sir, i am getting this...The parameters dictionary contains a null entry for parameter 'departmentId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index(Int32)' in 'DemoMVC.Controllers.EmployeeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.Parameter name: parameters

The error is because the departmentid is null in query string or in querystring there is no feild department id so u get this error. To avoid this u can use string ? departmentid instead of using string departmentid

Hi sir,Most of them are getting an error when following this tutorial.Do we need to change any settings in the RouteConfig.cs file.You are passing DepartmentId as parameters in ActionMethods and ActionLinks but there is no reference for that in the RouteConfig class.How does route engine will identity this[DepartmentId] parameter?

Error 1 'System.Data.Entity.DbSet' does not contain a definition for 'Where' and the best extension method overload 'System.Linq.Queryable.Where(System.Linq.IQueryable, System.Linq.Expressions.Expression>)' has some invalid arguments C:\Users\IBM_ADMIN\Documents\Visual Studio 2013\Projects\MVCDemoDataBase\MVCDemoDataBase\Controllers\EmployeeHyperLinkController.cs 26 49 MVCDemoDataBaseError 2 Argument 2: cannot convert from 'bool' to 'System.Linq.Expressions.Expression>' C:\Users\IBM_ADMIN\Documents\Visual Studio 2013\Projects\MVCDemoDataBase\MVCDemoDataBase\Controllers\EmployeeHyperLinkController.cs 26 81 MVCDemoDataBaseError 3 The name 'emp' does not exist in the current context C:\Users\IBM_ADMIN\Documents\Visual Studio 2013\Projects\MVCDemoDataBase\MVCDemoDataBase\Controllers\EmployeeHyperLinkController.cs 26 81 MVCDemoDataBaseError 4 The name 'emp' does not exist in the current context C:\Users\IBM_ADMIN\Documents\Visual Studio 2013\Projects\MVCDemoDataBase\MVCDemoDataBase\Controllers\EmployeeHyperLinkController.cs 26 88 MVCDemoDataBase

{SELECT [Extent1].[EmployeeId] AS [EmployeeId], [Extent1].[Name] AS [Name], [Extent1].[Gender] AS [Gender], [Extent1].[City] AS [City], [Extent1].[DepartmentId] AS [DepartmentId], [Extent1].[Departments_ID] AS [Departments_ID] FROM [dbo].[tblEmployee] AS [Extent1]}

I am getting this error of "Department_ID" but i haven't use this in my project. I have mentioned the code which i got from debug mode. Kindly rectify this asap.

The parameters dictionary contains a null entry for parameter 'departmentId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult index(Int32)' in 'MVCDemo.Models.EmployeeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.Parametre adı: parameters

The parameters dictionary contains a null entry for parameter 'depertid' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index(Int32)' in 'MVC_DEMO.Controllers.EmployeeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.Parameter name: parameters

The parameters dictionary contains a null entry for parameter 'departmentId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index(Int32)' in 'MVCDemo1.Controllers.DepartmentController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.Parameter name: parameters