What is ViewBag, ViewData and TempData in ASP.NET MVC Application

In ASP .Net MVC there are four type of Mechanism to Transfer data from Controller To views?

1.Viewdata
2.Viewbag
3.TempData
4.Using Strong Type passing Mechanisms

In this post i am going to explain What is ViewBag, ViewData, TempData and Using Strong Type passing Mechanisms in ASP.NET MVC Application one by one using example code.

What is ViewBag, ViewData and TempData in ASP.NET MVC Application

1. Viewdata

Introduced in MVC1 and MVC2, ViewData is a dictionary object that is derived from ViewDataDictionary class. ViewData variable will store data with object type.

There are three methods to assign data in view.
Syntax-
1)@ViewData[“VariableName”]
2)@ViewContext.ViewData[“VariableName”]
3)@ViewContext.Controller.ViewData[“VariableName”]

Ex:-ViewData [“x”] =10 // MVC 1 and MVC 2

Example :-

In Controller Code of ViewData

ViewData in View

OutPut:-

What is ViewBag, ViewData and TempData in ASP.NET MVC Application

Scope Of ViewData

  • lifetime of ViewData variable is current request only.

In above Example you have seen the output, to show scope of ViewData i used one button and that button will postback the current request, but when you click on submit button then values will be lost. To postback request in MVC we used [HttpPost] as given in below example.

Example Scope Of ViewData in Controller

Example Scope Of ViewData in View

Clicking on Submit button will lost the values.

What is ViewBag, ViewData and TempData in ASP.NET MVC Application

2. ViewBag

ViewBag is a dynamic type collection and it was introduced in MVC 3 (earlier we had ViewData). It does not require explicit conversion, because it perform task implicitly. Programming with ViewBag is “Weak Type” of Programming because there is :

  • No type checking.
  • No intellisense.
  • No auto Correction.
  • No early binding.
  • No design time error.

There are three methods to assign data in view.

1)@ViewBag[“VariableName”]
2)@ViewContext.ViewBag[“VariableName”]
3)@ViewContext.Controller.ViewBag[“VariableName”]

Ex:-ViewBag.x=10;

Dataset ds= ViewBag.x; [dynamically Conversion]

Scope Of ViewBag:

It is completely different  to “viewstate”, because viewstate concept removed completely from MVC. The scope of ViewBag is between a controller method and view only. we prepare data inside a method and redirect to view; view uses it and gets destroyed i.e. after giving results.

Controller Code below:

ViewPage  Code:

Difference between ViewData and ViewBag

the main difference bewteen ViewData and ViewBag  is ViewBag 
ViewBag supports Dynamic type  but ViewData doesnot.

3.TempData

TempData introduced in MVC1.0. TempData keeps the information for the time of an HTTP Request, This means only from one page to another. It helps to maintain the data to transfer from one controller to another controller or from one action to another action. It is derived from  TempDataDictionary class and it requires dynamic type conversion.

There are three methods to assign data in view.

1)@TempData[“VariableName”]
2)@ViewContext.TempData[“VariableName”]
3)@ViewContext.Controller.TempData[“VariableName”]

In Controllers

In View

OutPut:

What is ViewBag, ViewData and TempData in ASP.NET MVC Application

 

Scope Of TempData

Lifetime of TempDate variable is One usage only.it means once you used you can’t set in other variables. To make the TempData value even after one usage you have to use Keep method.

In Controllers

In View

Scenario 1 – Before Postback Request output will be

What is ViewBag, ViewData and TempData in ASP.NET MVC ApplicationIn the Scenario 1, I have taken two TempData X and Y in [HttpPost] at Controller and assigning   value of  a and c  to TempData X and Y as given below. Now definitely you have doubt that a and c values are stored in  TempData X and Y then what could be output ? May be according to you output will be X=Ron and Y=10.see the Scenario 2) for output

Scenario 2) After PostBack Request OutPut Will be-

What is ViewBag, ViewData and TempData in ASP.NET MVC Application

In the Scenario 2:here x=Ron that is correct but what about y value, y value should be 10. Let me clarify your doubt In TempData once you used  you can’t set to other variables. That means you have already use a= 10 and b=20.5 but i have not assign value of c. Once you click on submit button C is assign to X i.e X=Ron because c was not assigned to any TempData variable.

4.Using Strong Type passing Mechanisms

A dynamic type is strong but evaluated dynamic before execution. It introduced with C# 4.0.It is mostly preferable for Application development. You must use Model name space on the top of View page i.e. cshtml.

Ex- In Model

In Controller

In ViewPage

You can download all the code snippet i have given in this post from below link

ViewBag, ViewData and TempData Example Code Snippet

Please add important points from your side in the comments. Also if you are a developer and interested to submit any article, you can do so by visiting here Join CodingByte

7
SHARES

Rahul Kumar

Rahul is a ASP.Net Developer and having 2 years of Experience in G2C and e- Governance based Application development.

Related Post

1 Comment


  1. thanks for sharing,good article.I like it,I

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *