Pass Data From Controller To View In ASP.NET MVC

Here we will see how to pass data from controller to view. Before going to controller and view we need to do couple of things here.

Step 1: Create model class.

I have created Employee model class.

Create model class

Step 2: Create Controller

This is my EmployeeController class. Maybe its different your side:

Create Controller

Step 3: Create View

Here you can see I have created strongly typed view.

Create View

Step 4: Now we are ready to pass data from controller to view. In MVC we have four ways to pass data from controller to view.

  1. Go to Controller class which you have already created in previous step. Add namespace of model in controller class.

    Controller

  2. In MVC we have four ways to pass data from controller to view.

Option 1: In this way we pass model class object to the view. This is more appropriate option.

Here you can see I have just put some hard coded value to the employee class property and pass it to the view.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using DemoMVC.Models;  
  7.   
  8. namespace DemoMVC.Controllers {  
  9.     public class EmployeeController: Controller {  
  10.         //  
  11.         // GET: /Employee/  
  12.   
  13.         public ActionResult displayrecord() {  
  14.             Employee objemp = new Employee {  
  15.                 EmployeeID = 1001,  
  16.                     Name = "Priti kumari",  
  17.                     MobileNo = 8816705931,  
  18.                     Address = "Delhi"  
  19.             };  
  20.   
  21.             return View("Employee", objemp);  
  22.         }  
  23.   
  24.     }  
  25. }  
Now we need to access these data in View. I will go to my Employee.cshtml view and write code to access employee class property value.

->We need to ensure that view is strongly typed.
  1. @using DemoMVC.Models  
  2. @model DemoMVC.Models.Employee  
  3.   
  4. @{  
  5.     Layout = null;  
  6. }  
  7.   
  8. <!DOCTYPE html>  
  9.   
  10. <html>  
  11. <head>  
  12.     <meta name="viewport" content="width=device-width" />  
  13.     <title>Employee</title>  
  14. </head>  
  15. <body>  
  16.     <div>  
  17.        <b>Employee name:</b>  @Model.EmployeeID  
  18.         <br />  
  19.         <b> Employee Name:</b>  @Model.Name  
  20.         <br />  
  21.         <b> Mobile No:</b>  @Model.MobileNo  
  22.         <br />  
  23.         <b> Address:</b>   @Model.Address  
  24.         <br />  
  25.   
  26.     </div></body></html>  
In above code you can see how I access employee class property using @Model keyword. This the way to access model class property in razor view (.cshtml file).

After writing code we need to test its working or not. So now I am going to press F5 - Run application.

Run application

Here you can see output in browser window.

Option 2: Viewbag/ViewData –Using Viewbag/ViewData we can only pass data from controller action method to view. We can store data in ViewBag/ViewData for a single browser request.

 

Limitation

  1. You can only pass data from controller to view.
  2. Your data persist for a single browser request.

Let’s see this with example.

How to store data in ViewData/ViewBag

store data in viewdata

Here I have just assigned employee object to ViewBag/ViewData in controller class.

  1. ViewBag.EmployeeData = objemp; //objemp is employee class object  
  2. //OR You can use ViewBag OR ViewData.ViewData is more efficiant way as compare to ViewBag  
  3. ViewData["EmployeeData"] = objemp; //objemp is employee class object  
How to retrieve data from ViewBag/ViewData in view

Now we will see how we can retrieve data from ViewBag/ViewData.

In data access process you don't need to worry about how data is stored in action method ViewBag or Viewdata. You can just use any one process of accessing data.

retrieve data from ViewBag

Here is code:
I have cast ViewBage/ViewData to employee class because stored value is object type. In case of simple string value no need to cast ViewBage/ViewData.
  1. @{  
  2.    var emp1 = (Employee)ViewBag.EmployeeData;//retrive Data from ViewBag or ViewData  
  3.   
  4.    var emp = (Employee)ViewData["EmployeeData"];//retrive Data from ViewData or ViewBag  
  5. }  
Option 3: Tempdata- This is another important storage and data passing technique. We can use this technique for two different purpose.

 

  1. Passing data from Action Method to another action method like ViewState in ASP.NET Web form.
  2. Passing data from controller/Action method to the view.

TempData syntax for storing data in controller and retrieve data in View same as ViewData which I have shown in previous option 2.

Store value in Controller action method

  1. TempData["EmployeeTempData"] = objemp;//Store value in tempdata  
Retrieve value in View
  1. @{   
  2.    var emp = (Employee)TempData["EmployeeTempData"];//retrive Data from TempData  
  3. }  
Option 4: Now, here's last Session variable technique to store data. Using session variable you can store data for a single session and multiple request you persist for every request.

Syntax for session is same as ViewData/Tempdata . So you can refer Option 2 for more detail.

Store value in Controller action method:
  1. Session["EmployeeSessionData"] = objemp;  
Retrieve value in View:
  1. @{  
  2.    var emp = (Employee)Session["EmployeeSessionData"];  
  3. }  
Thanks for reading my article.

 


View All Comments