Introduction
In this article, we will see different ways of passing the Model data from Controller to View. We know to pass the data from Controller to View we use View() method to return ViewResult return type. Apart from View() method we can use different ways to return the data.
There are three different ways of returning the data from Controller to View or .cshtml file as shown below.
ViewData
ViewData is used for returning the data from Controller's action method to View or .cshtml file. ViewData is a dictionary or it's a property with get & set accessors, which is part of ControllerBase class. ViewDataDictionary class inherits from IDictionary<string, object> interface and ViewData returns ViewDataDictionary class object, that means ViewData is a collection of Dictionary objects with string, object types. ViewData's data can be accessed in the current page instance. If there is any navigation from one view to another view then there may be chances of losing the data from ViewData object.
In Controller's Action Method in order to assign the Model data to ViewData dictionary object, in the below syntax I am using employee object as well as list of employee objects to store in ViewData dictionary object and return ViewData to the View or .cshtml and binds it to the HTML table.
Syntax
- Employee employeeObj = new Employee();
- ViewData["EmployeeObject"] = employeeObj;
From the above example on the right hand side we need to pass or assign the employee object which we get from database and stored in Model class.
If we want to assign the list of Employee object we need to assign like below.
- List<Employee> listOfEmployees = new List<Employee>();
- ViewData["EmployeeListObject"] = listOfEmployees;
Here on the right hand side we need to pass the list of employee object, if we want to pass the list of employees from Controler to View or .cshtml file.
ViewBag
ViewBag is used for returning the data from Controller to View. ViewBag is a property with only get accessor which is a member of ControllerBase abstract class with dynamic type. It is similar to ViewData. ViewBag object data can be accessed in the current page instance. If there is any navigation from one view to another view then there may be chances of losing the data from ViewBag object.
In Controller's Action Method in order to assign the Model data to ViewBag object we assign as shown below. In the below syntax, I am using employee object as well as a list of employee objects to store the data in ViewBag object and return ViewBag to the View or .cshtml and bind it to the HTML table.
Syntax
- Employee employeeObj = new Employee();
- ViewBag.EmployeeObject = employeeObj;
From the above example on the right hand side we need to pass the employee object which we get from database and stored in Model class.
If we want to assign the list of Employee objects we need to assign like below.
- List<Employee> listOfEmployees = new List<Employee>();
- ViewBag.EmployeeListObject = listOfEmployees;
Here on the right hand side we need to pass the list of employee objects, if we want to pass the list of employees from controller to view or .cshtml file.
TempData
Using TempData we can pass the data from Controller's Action Method to View or .cshtml file. It is similar to ViewData.
TempData is a dictionary of objects or a property with get and set accessors which is a member of ControllerBase abstract class.
TempData has a return type TempDataDictionary class and TempDataDictionary class inherits from
IDictionary<string, object> interface that means TempData is a collection of Dictionary object with string, object types.
Whenever there is any navigation from one View to another View, TempData object's data will be available and can be accessed in redirection of pages.
It is different from ViewData and ViewBag, In ViewData and ViewBag whenever there is any navigation or redirection of webpage then the data will not be available.
But the data will be available even during the redirection of webpages in TempData.
In Controller's Action Method in order to assign the Model data to TempData object, in the below syntax, I am using employee object as well as list of employee objects to store in TempData object and return TempData to the View or .cshtml and bind the data to the HTML table.
Syntax
- Employee employeeObj = new Employee();
- TempData["EmployeeObject"] = employeeObj;
From the above example on the right hand side we need to pass the employee object which we get from database and stored in Model class.
If we want to assign the list of Employee object we need to assign like below.
- List<Employee> listOfEmployees = new List<Employee>();
- TempData["EmployeeListObject"] = listOfEmployees;
Here on the right hand side we need to pass the list of employee objects, if we want to pass the list of employees from Controler to View or .cshtml file.
Let's Create ASP.NET MVC 5 Application and Create a Project in order to understand ViewData, ViewBag & TempData.
I am using Visual Studio 2019 community edition to create a sample project, you can use any version of your choice.
Step 1: Open Visual Studio 2019 and click on Create a new project on the bottom right as shown below.
Step 2: Search ASP.NET Web Application in the above-provided textbox and click on the template and click on the Next button as shown below.
Step 3: Provide the Project name as you wish, I have used project name as MVCFirstProject and click on Create button.
Step 4: Select the MVC template and click on the Create button, with this a new MVC project is Created.
Once the ASP.NET 5 Application is created, we need to Create a Model to use in our application, in this example I will assign the object with some hardcoded values.
In order to create a Model, right click on Models folder then click on Add, select the class template and give the name as Employee.cs and click on Add button.
With this Employee.cs Model will be created in Models folder.
Open the Employee.cs file and paste the properties as shown below.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
-
- namespace MVCViewData.Models
- {
- public class Employee
- {
- public int EmployeeId { get; set; }
- public string EmployeeName { get; set; }
- public string EmployeeDesignation { get; set; }
- public string DateOfJoining { get; set; }
- public int EmployeePhone { get; set; }
- public string EmployeeGender { get; set; }
- public string City { get; set; }
- public string Project { get; set; }
- public string CompanyName { get; set; }
- }
- }
In order to create a EmployeeController, right click on Controller's folder, click on Add and click on Controller, with this Add Scaffold template will be opened, in this
select mvc 5 Controller - Empty and click on Add button. In Add Controller window, give the Controller name as EmployeeController and click on Add button. With this
EmployeeController will be created in Controllers folder.
Now open the EmployeeController.cs class and paste the below code.
- using MVCViewData.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
-
- namespace MVCViewData.Controllers
- {
- public class EmployeeController : Controller
- {
- List<Employee> listEmployee = new List<Employee>();
- Employee employee = new Employee();
-
- public ActionResult ViewDataTest()
- {
- AssigningValues();
- ViewData["EmployeeInfo"] = employee;
- ViewData["EmployeesInfo"] = listEmployee;
-
- return View();
- }
-
- public ActionResult ViewBagTest()
- {
- AssigningValues();
- ViewBag.EmployeeInfo = employee;
- ViewBag.EmployeesInfo = listEmployee;
-
- return View();
- }
-
- public ActionResult TempDataTest()
- {
- AssigningValues();
- TempData["EmployeeInfo"] = employee;
- TempData["EmployeesInfo"] = listEmployee;
-
- return View();
- }
-
- private void AssigningValues()
- {
- employee.EmployeeId = 1001;
- employee.EmployeeName = "Khaja";
- employee.EmployeeDesignation = "SE";
- employee.DateOfJoining = "22/05/2015";
- employee.EmployeePhone = 806980474;
- employee.EmployeeGender = "Male";
- employee.City = "Hyderabad";
- employee.Project = "AILabz";
- employee.CompanyName = "ValueLabs";
-
-
-
-
-
- listEmployee.Add(new Employee()
- {
- EmployeeId = 1001,
- EmployeeName = "Khaja",
- EmployeeDesignation = "SE",
- DateOfJoining = "22/05/2015",
- EmployeePhone = 806980474,
- EmployeeGender = "Male",
- City = "Hyderabad",
- Project = "AILabz",
- CompanyName = "ValueLabs",
- });
-
- listEmployee.Add(new Employee()
- {
- EmployeeId = 1002,
- EmployeeName = "Vijay",
- EmployeeDesignation = "S.SE",
- DateOfJoining = "10/05/2013",
- EmployeePhone = 907846532,
- EmployeeGender = "Male",
- City = "Hyderabad",
- Project = "AutomationTool",
- CompanyName = "Accenture",
- });
-
- listEmployee.Add(new Employee()
- {
- EmployeeId = 1003,
- EmployeeName = "Vikas",
- EmployeeDesignation = "TeamLead",
- DateOfJoining = "01/10/2012",
- EmployeePhone = 798534120,
- EmployeeGender = "Male",
- City = "Bangalore",
- Project = "AzureDevOps",
- CompanyName = "Cognizant",
- });
- }
- }
- }
In the above code I have an Employee as well List<Employee> objects. In EmployeeController we have three different Action methods as well as a normal method AssigningValues() like
ViewDataTest
ViewBagTest &
TempDataTest
In these Action Methods we are using three different ways of returning the data to the View() or .cshtml file as shown above.
In AssigningValues method, I am assigning the Employee object and List of Employees objects. In ViewDataTest() method i am calling AssigningValues() method, with this Employee and list of Employee objects will be assigned with data. With this data, I am assigning it to ViewData dictionary object.
Similarly for ViewBagTest and TempDataTest I am doing the same thing what I did for ViewDataTest() method.
Whenever we create a Controller say EmployeeController with three different action methods, then a new folder will be created in Views folder with three different Views or .cshtml files
The folder which will be created in Views folder has the same name as Controller's name and View() or .cshtml files have the same name as Action Methods.
The Folder structure of Views as shown below.
Views--> Employee-->ViewDataTest.cshtml
Views--> Employee-->ViewBagTest.cshtml
Views--> Employee-->TempDataTest.cshtml
In order to create ViewDataTest.cshtml View, right click on the ViewDataTest Action method, click on Add View, with this AddView window will be opened, give the View Name as ViewDataTest and click on Add.
With this ViewDataTest.cshtml will be created, now open the ViewDataTest.cshtml file and paste the below code.
- @{
- ViewBag.Title = "ViewDataTest";
- var employeeInfo = ViewData["EmployeeInfo"] as MVCViewData.Models.Employee;
- }
-
- <h2>ViewDataTest</h2>
-
- <hr>
-
- <h3>Employee Information Using ViewData</h3>
- <table>
- <tr>
- <th>Employee Id</th>
- <th>Employee Name</th>
- <th>Employee Designation</th>
- <th>Date Of Joining</th>
- <th>Employee Phone</th>
- <th>Employee Gender</th>
- <th>City</th>
- <th>Project</th>
- <th>Company Name</th>
- </tr>
-
- <tr>
- <td>@employeeInfo.EmployeeId</td>
- <td>@employeeInfo.EmployeeName</td>
- <td>@employeeInfo.EmployeeDesignation</td>
- <td>@employeeInfo.DateOfJoining</td>
- <td>@employeeInfo.EmployeePhone</td>
- <td>@employeeInfo.EmployeeGender</td>
- <td>@employeeInfo.City</td>
- <td>@employeeInfo.Project</td>
- <td>@employeeInfo.CompanyName</td>
- </tr>
- </table>
-
- <h3>Employees Information Using ViewData</h3>
- <table>
- <tr>
- <th>Employee Id</th>
- <th>Employee Name</th>
- <th>Employee Designation</th>
- <th>Date Of Joining</th>
- <th>Employee Phone</th>
- <th>Employee Gender</th>
- <th>City</th>
- <th>Project</th>
- <th>Company Name</th>
- </tr>
-
- @foreach (var eachEmployee in ViewData["EmployeesInfo"] as List<MVCViewData.Models.Employee>)
- {
- <tr>
-
- <td>@eachEmployee.EmployeeId</td>
- <td>@eachEmployee.EmployeeName</td>
- <td>@eachEmployee.EmployeeDesignation</td>
- <td>@eachEmployee.DateOfJoining</td>
- <td>@eachEmployee.EmployeePhone</td>
- <td>@eachEmployee.EmployeeGender</td>
- <td>@eachEmployee.City</td>
- <td>@eachEmployee.Project</td>
- <td>@eachEmployee.CompanyName</td>
- </tr>
- }
-
- </table>
-
-
-
- <style>
- table {
- font-family: arial, sans-serif;
- border-collapse: collapse;
- width: 100%;
- }
-
- td, th {
- border: 1px solid #dddddd;
- text-align: left;
- padding: 8px;
- }
- </style>
In the above code I used var employeeInfo = ViewData["EmployeeInfo"] as MVCViewData.Models.Employee;
The ViewData["EmployeeInfo"] will have Employee object which is passed from ViewDataTest Controller. Here I am Converting ViewData["EmployeeInfo"] as Employee.cs Model and assigning it to employeeInfo.
Similarly in the above code I used @foreach (var eachEmployee in ViewData["EmployeesInfo"] as List<MVCViewData.Models.Employee>).
Here ViewData["EmployeesInfo"] data will be getting from ViewDataTest Action Method. I am converting ViewData["EmployeesInfo"] as List<Employee> object and iterating one by one through list of employees and binding it to the HTML table.
In order to create ViewBagTest.cshtml View, right click on the ViewBagTest Action Method, click on Add View, with this AddView window will be opened, give the View Name as ViewBagTest and click on Add.
With this ViewBagTest.cshtml will be created, now open the ViewBagTest.cshtml file and paste the below code.
- <h2>ViewBagTest</h2>
-
- <hr>
-
- @{
- ViewBag.Title = "ViewBagTest";
- var employeeInfo = ViewBag.EmployeeInfo as MVCViewData.Models.Employee;
- }
-
-
- <h3>Employee Information Using ViewBag</h3>
- <table>
- <tr>
- <th>Employee Id</th>
- <th>Employee Name</th>
- <th>Employee Designation</th>
- <th>Date Of Joining</th>
- <th>Employee Phone</th>
- <th>Employee Gender</th>
- <th>City</th>
- <th>Project</th>
- <th>Company Name</th>
- </tr>
-
- <tr>
- <td>@employeeInfo.EmployeeId</td>
- <td>@employeeInfo.EmployeeName</td>
- <td>@employeeInfo.EmployeeDesignation</td>
- <td>@employeeInfo.DateOfJoining</td>
- <td>@employeeInfo.EmployeePhone</td>
- <td>@employeeInfo.EmployeeGender</td>
- <td>@employeeInfo.City</td>
- <td>@employeeInfo.Project</td>
- <td>@employeeInfo.CompanyName</td>
- </tr>
- </table>
-
- <h3>Employees Information Using ViewBag</h3>
- <table>
- <tr>
- <th>Employee Id</th>
- <th>Employee Name</th>
- <th>Employee Designation</th>
- <th>Date Of Joining</th>
- <th>Employee Phone</th>
- <th>Employee Gender</th>
- <th>City</th>
- <th>Project</th>
- <th>Company Name</th>
- </tr>
-
- @foreach (var eachEmployee in ViewBag.EmployeesInfo as List<MVCViewData.Models.Employee>)
- {
- <tr>
-
- <td>@eachEmployee.EmployeeId</td>
- <td>@eachEmployee.EmployeeName</td>
- <td>@eachEmployee.EmployeeDesignation</td>
- <td>@eachEmployee.DateOfJoining</td>
- <td>@eachEmployee.EmployeePhone</td>
- <td>@eachEmployee.EmployeeGender</td>
- <td>@eachEmployee.City</td>
- <td>@eachEmployee.Project</td>
- <td>@eachEmployee.CompanyName</td>
- </tr>
- }
-
- </table>
- <style>
- table {
- font-family: arial, sans-serif;
- border-collapse: collapse;
- width: 100%;
- }
-
- td, th {
- border: 1px solid #dddddd;
- text-align: left;
- padding: 8px;
- }
- </style>
Similarly to Create TempDataTest.cshtml View, right click on the TempDataTest Action Method, click on Add View, with this AddView window will be opened, give the View Name as TempDataTest and click on Add.
With this TempDataTest.cshtml will be created, now open the TempDataTest.cshtml file and paste the below code.
- @{
- ViewBag.Title = "TempDataTest";
- var employeeInfo = TempData["EmployeeInfo"] as MVCViewData.Models.Employee;
- }
-
- <h2>TempDataTest</h2>
- <hr>
-
- <h3>Employee Information Using TempData</h3>
- <table>
- <tr>
- <th>Employee Id</th>
- <th>Employee Name</th>
- <th>Employee Designation</th>
- <th>Date Of Joining</th>
- <th>Employee Phone</th>
- <th>Employee Gender</th>
- <th>City</th>
- <th>Project</th>
- <th>Company Name</th>
- </tr>
-
- <tr>
- <td>@employeeInfo.EmployeeId</td>
- <td>@employeeInfo.EmployeeName</td>
- <td>@employeeInfo.EmployeeDesignation</td>
- <td>@employeeInfo.DateOfJoining</td>
- <td>@employeeInfo.EmployeePhone</td>
- <td>@employeeInfo.EmployeeGender</td>
- <td>@employeeInfo.City</td>
- <td>@employeeInfo.Project</td>
- <td>@employeeInfo.CompanyName</td>
- </tr>
- </table>
-
- <h3>Employees Information Using TempData</h3>
- <table>
- <tr>
- <th>Employee Id</th>
- <th>Employee Name</th>
- <th>Employee Designation</th>
- <th>Date Of Joining</th>
- <th>Employee Phone</th>
- <th>Employee Gender</th>
- <th>City</th>
- <th>Project</th>
- <th>Company Name</th>
- </tr>
-
- @foreach (var eachEmployee in TempData["EmployeesInfo"] as List<MVCViewData.Models.Employee>)
- {
- <tr>
-
- <td>@eachEmployee.EmployeeId</td>
- <td>@eachEmployee.EmployeeName</td>
- <td>@eachEmployee.EmployeeDesignation</td>
- <td>@eachEmployee.DateOfJoining</td>
- <td>@eachEmployee.EmployeePhone</td>
- <td>@eachEmployee.EmployeeGender</td>
- <td>@eachEmployee.City</td>
- <td>@eachEmployee.Project</td>
- <td>@eachEmployee.CompanyName</td>
- </tr>
- }
-
- </table>
- <style>
- table {
- font-family: arial, sans-serif;
- border-collapse: collapse;
- width: 100%;
- }
-
- td, th {
- border: 1px solid #dddddd;
- text-align: left;
- padding: 8px;
- }
- </style>
Now build and run the application by navigating to three different URL's like below.
http://localhost:51586/Employee/ViewDataTest
http://localhost:51586/Employee/ViewBagTest
http://localhost:51586/Employee/TempDataTest
When the user navigates to the first URL then the EmployeeController ViewDataTest will handle the request and give the result as shown below.
When user navigates to the second URL, then EmployeeController ViewBagTest will handle the request and gives the result as shown below.
Similarly when user navigates to the third URL, then EmployeeController TempDataTest will handle the request and gives the result as shown below.
Thanks for reading my article, I hope this article helps you.