Click here to Skip to main content
15,885,366 members
Articles / Web Development / ASP.NET
Tip/Trick

In ASP.NET MVC Passing Results of LINQ to View

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
10 Dec 2016CPOL2 min read 23.3K   5   4
Solution for passing data from View to controller, contains useful concepts for beginner developers

Introduction

This tip/trick shows a simple way for passing data between Controller and View. The current tip/trick shows how to pass the result of LINQ query to View, and show its results using foreach loop.

Background

ASP.NET MVC is an advanced technology from Micrososft for developing web sites, that it provides full separation between View, Controller, and Model layers.

Using the Code

This solution shows few concepts related to ASP.NET can be a good example for beginners in this field. This solution has ideas and solutions related to LINQ , Entity Framework.

First, add item ADO Entity Data Model, and create models on this file using the database.

Image 1

Above is the ADO Entity Model after generating the models from the database, the model consists of two tables that have one to many relation. The department has many relations with Employee. The ADO Entity Model automatically generates the layers of the Model classes.

In this solution, the employees of a certain department will be returned, the ViewModel will be created to show the properties of Employees of department and properties of the related department.

The ViewModel contains property of type IList, to receive the results of inner Join query of LINQ, and display its contents on View.

C#
public class EmployeeDepartment
{
 public string departmentName {get;set;}
 public string departmentLocation {get;set;}
 public string FirstName {get;set;}
 public string LastName {get;set;}
 public IList EmployeeDepartment MenuItems{get;set;}
}

The controller is responsible for processing the data that is retrieved from the model and passing it to View. So, the controller is the heart of ASP.NET MVC. Passing values from controller to View can be binding controller to view, or using properties using ViewBag, ViewData, and TempData.

C#
public class EmployeeController : Controller
    {
     // GET: Employee
     public ActionResult Index()
     {
         return View();
     }
        public ViewResult ViewEmployee(int DepartmentID)
        {
             EntityTestEntities context = new EntityTestEntities();
             EmployeeDepartment empDepartment = new EmployeeDepartment();

            empDepartment.MenuItems = (from employee in context.Employees
                                        join department in context.Departments
                                        on employee.Dep_Id equals department.ID
                                        where employee.Dep_Id == DepartmentID
                                        select new EmployeeDepartment()
                                       {
                                            FirstName=employee.FirstName,
                                           LastName=employee.LastName,
                                           departmentName=department.Location,
                                           departmentLocation=department.Name
                            
                                        }).ToList();    
         return View(empDepartment);
     }
 }

The controller EmployeeController uses the inner join query to retrieve the employees of a certain department, the search uses the Id of the department that is passed using URL and received by the action ViewEmployee. After that, a view is created for the ViewEmployee, that is of type List and the Model class is ViewModel that is DepartmentEmployee class.

Image 2

    @model EntityMVCTest1.Models.EmployeeDepartment
    @{
     ViewBag.Title = "ViewEmployee";
    }    

<h2>ViewEmployee</h2>

<ul>
    <li>@foreach (var item in Model.MenuItems) {</li>
    <li>Employee First Name : @item.FirstName</li>
    <li>Employee Last Name : @item.LastName</li>
    <li>}</li>
</ul>

<p>The markup at ViewEmployee.cshtml starts by identifying the Model of the view, 
that is the EmployeeDepartment.After that the Markup uses the object passed by the action of 
controller to iterate the value through the foreach loop</p>

Image 3

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior)
Jordan Jordan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionTHANKS Pin
Eren Alkan 20226-May-22 7:18
Eren Alkan 20226-May-22 7:18 
Praiseuseful Pin
Member 125339823-Dec-19 4:57
Member 125339823-Dec-19 4:57 
PraiseVery well explained Pin
Member 141062962-Jan-19 11:40
Member 141062962-Jan-19 11:40 
QuestionSome thoughts... Pin
Middle Manager13-Dec-16 6:05
Middle Manager13-Dec-16 6:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.