Click here to Skip to main content
15,886,806 members
Articles / Web Development / ASP.NET / ASP.NET4.0

Passing Data Using ViewData, ViewBag, TempData, Session Variables In ASP.NET MVC

Rate me:
Please Sign up or sign in to vote.
2.14/5 (8 votes)
5 Jan 2018CPOL3 min read 14.6K   6   1
In ASP.NET web application we are familiar with data passing and state management mechanism like Session, ViewState , hiddenfield etc. But in ASP.NET MVC Application we have ViewData, ViewBag, TempData, Session Variables.

Introduction

In this article, we will learn Passing Data Using ViewData, ViewBag, TempData, Session Variables In ASP.NET MVC

Background

In ASP.NET web application we are familiar with data passing and state management mechanism like Session, ViewState , hidden fields etc. But in ASP.NET MVC Application we don't have server control available so there is no ViewState or hidden fields .

 

Using the code

In ASP.NET web application we are familiar with data passing and state management mechanism like Session, viewstate , hiddenfield etc. But in ASP.NET MVC Application we don't have server control available so there is no viewstate or hiddenfield .

For passing data from Controller to View in ASP.NET MVC we have ViewData and ViewBag.

Assembly : System.Web.Mvc.DynamicViewDataDictionary

Let's understand the concept and usage of ViewData/ViewBag practically.

Create a new MVC Application and select Empty Template as in the following screenshot:



Now, right click on Controller folder, Add, then click controller and add 'HomeController' in your application.

Select 'MVC 5 controller - Empty'.

 

Now, Add View for the 'HomeController', for that right click on Action Method Index and click on 'Add View'.

 

 

Give 'View name' as 'Index' as in the following screenshot and click 'OK'.

Now write the following code in your ActionResult Index.

Access the ViewData in View 'Index' as in the following screenshot:

 

Test your data by running the application, and it will display the message which you passed from Controller to view.

 

Now, same way we can pass data from Controller to View ysing ViewBag.

 

ViewBag Syntax is easier than ViewData, no need to write extra [] brackets, It's simple, so now write the following code in your 'Homecontroller'

C++
ViewBag.MyData = "This is ViewBag Example.";  

and call ViewBag from View 'Index', write the following code.

C++
<h1>@ViewBag.MyData</h1> 

Check your code by running your application.

 

 

What if you want to pass data from one action to another action, ViewData or ViewBag won't work here, it does not maintain data from action to action, so you need TempData.

Using TempData we can pass data from one controller to another controller, TempData keeps the value for the time of an HTTP Request. Using Return RedirectToAction("TempDataEx") we can redirect from one action to another action.

C++
public class HomeController : Controller  
   {  
       // GET: Home  
       public ActionResult Index()  
       {  
          //ViewData["Msg"] = "This is ViewData Example.";  
          // ViewBag.MyData = "This is ViewBag Example.";  
           
           TempData["TempModel"] = "This is TempData Example";  
           return RedirectToAction("TempDataEx");  
          // return View();  
       }  
  
       public ActionResult TempDataEx()  
       {  
             
           return View();  
       }  

Add new View 'TempDataEx' and access TempData["TempModel"] value in this view.

 

Here we have passed data from Index Controller to TempDataEx  Controller.

 

Let's check Session, it works same as the ASP.NET session and maintain data until you close the browser. Now set the session in Index Method and access in both view Index and TempDataEx.

Run the application and check both the View.

 

 

 

You can get session data in both the view.

Points of Interest

ViewData and ViewBag: You can pass data from Controller to View for the 1st request. The difference between ViewData and ViewBag is,

  • The syntax of ViewData is easy to use and ViewBag use the C# 4 feature called Dynamic.
  • ViewData requires typecasting and check for null values, ViewBag do not require typecasting.

We cannot pass data from Action to Action using ViewData and ViewBag.

TempData: You can pass data from one controller to another controller or Action to Action. 

Session: You can maintain the data until you close the browser. it's same like ASP.NET Session.

It’s important in MVC to Choose your Data passing method wisely don’t use Session all the time for passing data.

History

This is my first article on CodeProject, I hope you like it.

License

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


Written By
Team Leader
India India
Ankur Mistry is working as a Scrum Master (CSM), He is C#Corner MVP (2017 and 2016), Speaker, CSP and Masters in Computer Application. Having 10 + years of experience in IT and his area of interest is C#, ASP.NET, SQL Server, Project Management, Scrum, Agile, crystal reports, MVC4, MVC5, WCF, Web Services, SSRS, HTML etc.HTML etc.

Comments and Discussions

 
Questionasp.net mvc Pin
Member 1455297619-Aug-19 0:13
Member 1455297619-Aug-19 0:13 

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.