ViewBag Vs ViewData Vs TempData

In this article we will learn what is ViewData, ViewBag and TempData in ASP.NET MVC. We will talk about each and every step by step  definition with practically.

Let us create an ASP.NET application.

Open Visual Studio go to below step. 

File->New->Project with name "ViewbagVsViewDataVsTempData"

Click on OK button then below dialog box will appear.

Now select empty template and then click on OK button and then our project is created successfully. Now add a controller. Right click on controller and choose Add and then select Controller. Now dialog box will appear below.

Click over Controller and add controller with name "CheckDemo" then below dialog box will appear.

Click Add button. Now create a Action method which name “SayHello,” and it contains the information which we will pass in view available in controller.
For passing the information from controller to view here we will use View data.
View Data: It is an object of ViewDataDictionary type. It contains the value like array notation.
Here view data contains values like:
ViewData["username"] = name;
To show the value using ViewData we need to add View.

Add View: Right click on view and click Add View like, 

After that view shows like this.

After that I have write some message like. 

      ViewBag.Title = "SayHello";  
<h2>Hi, @ViewData["username"].ToString() Welcome to first MVC Application.</h2>  

Here I have to type cast ViewData into string because it returns an object of ViewDataDictionary type.

Now I will execute my application. Then an error will come like this:  

Because we must have to pass controller name and ActionResult name and its parameter, h here we pass controller name and ActionResult name and its parameter like this: 

will be show After that output like.


It is a wrapper for viewdata. It performs same functionality but it is a dynamic property that takes advantage of the new dynamic features in C# 4.0. It is dot(.) notation . It does not need to type cast. 

Same ActionResult I used ViewBag like.  Code is given beow.

  namespace ViewbagVsViewDataVsTempData.Controllers  
      public class CheckDemoController : Controller  
        public ActionResult SayHello(string id)  
              //ViewData["username"] = id;  
              ViewBag.usertname = id;  
              return View();  


It is used for passing data from one controller to another controller or one request to other request. TempData contains the data until target is completely loaded. TempData is a dictionary object that is derived from TempDataDictionary class and stored in short live sessions.
It is also needs to type cast 
For TempData I create some Actionresult like and view. Here we store the data in TempData for passing one controller to another controller. 

      public ActionResult CreateForm()  
          return View();  

        public ActionResult CreateForm(string id)  
          TempData["username"] = id;  
          return RedirectToAction("ShowMessage");  

Add view of Create a form Controller:

 ViewBag.Title = "CreateForm";  
  <h2>Create User</h2>  
  @using (Html.BeginForm()) {   
          <input id="name" type="text"/>  
          <p><input id="btncreate" type="submit" value="Create"> </p>  

This view shows like this:

Those values enter this textbox that value stores in TempData and shows tempdata value in other ActionResult (ShowMessage) view.

And Add Another Action result here for showing the TempData stored value.

public ActionResult ShowMessage()  
      return View();  

  @{      ViewBag.Title = "ShowMessage";  
  <h2>Hi, @TempData["username"].ToString()  Welcome to first MVC Application using TempData.</h2> 
Add caption
The first time completely load view with TempData, after that tempdata is lost. For checking  it when we refresh a page then we get an error like: 
This error proves completely loaded TempData;  after that data is lost.

Conclusion: In this article we have learnt that what is ViewBag, ViewData and TempData and how to use it step by step practically.

No comments

Powered by Blogger.