Passing Data From Controller To View Using Viewdata In ASP.NET MVC 5

In this article will perform a simple demonstration of how we can pass data from controller to view. In this demo will perform this activity using Viewdata, which is a dictionary, derived from view data dictionary class. Viewdata basically holds the data which will be passed from controller to view.

Let's start our demo

Let's create a controller



Create a demo class PersonDetails, consisting of different parameters of details like name, age, email and city.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace ProjectMVC.Models  
  7. {  
  8.     public class Employee  
  9.     {  
  10.         public string _name { getset; }  
  11.         public int _age { getset; }   
  12.         public string _email { getset; }  
  13.         public string _city { getset; }  
  14.     }  
  15. }  
After adding a model class,here is how we can get that model value in controller after creating object of that model class in the action of the controller. Make sure there is Namespace for recently added model so that we can get the values of recently added model class in controller.
  1. using ProjectMVC.Models;  
Add following piece of code in controller's action, where we create the object of the class, which will get all the values(attributes) of the class into controller. We got values from model to controller.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Web;  
  4. using System.Web.Mvc;  
  5. using ProjectMVC.Models;  
  6.   
  7. namespace ProjectMVC.Controllers  
  8. {  
  9.     public class PersonController : Controller  
  10.     {        
  11.         public ActionResult GetEmployeeDetails()  
  12.         {  
  13.             Employee empdetails = new Employee();  
  14.             empdetails._name = "Shridhar Sharma";  
  15.             empdetails._age = 25;  
  16.             empdetails._email = "ishriss@c-sharpcorner.com";  
  17.             empdetails._city = "New Delhi";  
  18.             ViewData["Employeedetails"] = empdetails;               
  19.             return View("employeeview");  
  20.         }  
  21.     }  
  22. }  
using Viewdata: Here we can store model data into viewdata so that we can retrieve on view.
  1. ViewData["Employeedetails"] = empdetails;     
Add view



Now let's move into final step i.e, getting model data into view using viewdata.
  1. @{  
  2.     Layout = null;  
  3.     ProjectMVC.Models.Employee empdetails = (ProjectMVC.Models.Employee)  
  4.         ViewData["Employeedetails"];  
  5. }  
  6.   
  7. <!DOCTYPE html>  
  8.   
  9. <html>  
  10. <head>  
  11.     <meta name="viewport" content="width=device-width" />  
  12.     <title>employeeview</title>  
  13. </head>  
  14. <body>  
  15.     <table>  
  16.         <tr>  
  17.             <td>NAME</td>  
  18.             <td>AGE</td>  
  19.             <td>EMAIL</td>  
  20.             <td>CITY</td>  
  21.         </tr>  
  22.         <tr>  
  23.             <td>@empdetails._name</td>  
  24.             <td>@empdetails._age</td>  
  25.             <td>@empdetails._email</td>  
  26.             <td>@empdetails._city</td>  
  27.     </table>  
  28. </body>  
  29. </html>  
Output View:
 

Closure 

We did a simple exercise how we can pass data from controller to view,So here is recap..we add controller,after that we added a model class consisting of attributes, got those attributes in controller by creating object of that model class, after that we passed that data overview by using viewdata.

Hope you like this.