[go: up one dir, main page]

0% found this document useful (0 votes)
87 views3 pages

ViewData Vs ViewBag Vs TempData Vs Session

This article explains the differences between ViewData, ViewBag, TempData and Session in ASP.NET MVC. ViewData and ViewBag are used to pass data from controller to view during the current request. TempData passes data from current to subsequent request, persisting for that one redirect. Session persists data across all requests.

Uploaded by

Raghu Gowda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views3 pages

ViewData Vs ViewBag Vs TempData Vs Session

This article explains the differences between ViewData, ViewBag, TempData and Session in ASP.NET MVC. ViewData and ViewBag are used to pass data from controller to view during the current request. TempData passes data from current to subsequent request, persisting for that one redirect. Session persists data across all requests.

Uploaded by

Raghu Gowda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

6/28/2014 ViewData vs ViewBag vs TempData vs Session

ViewData vs ViewBag vs TempData vs Session


P o st e d By : S h aile ndra Ch auh an, 1 9 Jul 2 0 1 2
U pdat e d On : 2 4 Jun 2 0 1 4
V e rs i o n Su p p o rt : MV C5 , MV C4 & MV C3

Ke y wo rd s : wh e n t o u s e v i e wb a g , v i e wd a t a a n d t e mp d a t a , d i ffe re n ce s b e t we e n
v i e wd a t a , v i e wb a g , s e s s i o n a n d t e mp d a t a i n mv c3 mv c4 , t e mp d a t a k e e p me t h o d

I n ASP.NET MVC there are three ways - ViewData, ViewBag and TempData to pass data from controller
to view and in next request. Like WebForm, you can also use Session to persist data during a user
session. Now question is that when to use ViewData, VieBag, TempData and Session. Each of them has its
own importance. In this article, I am trying to explain the differences among these four.

ViewData

01. ViewData is a dictionary object that is derived from ViewDataDictionary class.

1. public ViewDataDictionary ViewData { get; set; }

02. ViewData is a property of ControllerBase class.

03. ViewData is used to pass data from controller to corresponding view.

04. It’s life lies only during the current request.

05. If redirection occurs then it’s value becomes null.

06. It’s required typecasting for getting data and check for null values to avoid error.

ViewBag

01. ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.

02. Basically it is a wrapper around the ViewData and also used to pass data from controller to
corresponding view.

1. public Object ViewBag { get; }

03. ViewBag is a property of ControllerBase class.

04. It’s life also lies only during the current request.

05. If redirection occurs then it’s value becomes null.

06. It doesn’t required typecasting for getting data.

http://www.dotnet-tricks.com/Tutorial/mvc/9KHW190712-ViewData-vs-ViewBag-vs-TempData-vs-Session.html 1/3
6/28/2014 ViewData vs ViewBag vs TempData vs Session

TempData

01. TempData is a dictionary object that is derived from TempDataDictionary class and stored in short lives

session.

1. public TempDataDictionary TempData { get; set; }

02. TempData is a property of ControllerBase class.

03. TempData is used to pass data from current request to subsequent request (means redirecting from

one page to another).

04. It’s life is very short and lies only till the target view is fully loaded.

05. It’s required typecasting for getting data and check for null values to avoid error.

06. It is used to store only one time messages like error messages, validation messages. To persist data

with TempData refer this article: Persisting Data with TempData

Session

01. Session is an object that is derived from HttpSessionState class.

1. public HttpSessionState Session { get; }

02. Session is a property of HttpContext class.

03. Session is also used to pass data within the ASP.NET MVC application and Unlike TempData, it never

expires.

04. Session is valid for all requests, not for a single redirect.

05. It’s also required typecasting for getting data and check for null values to avoid error.

http://www.dotnet-tricks.com/Tutorial/mvc/9KHW190712-ViewData-vs-ViewBag-vs-TempData-vs-Session.html 2/3
6/28/2014 ViewData vs ViewBag vs TempData vs Session

Summary
In this article I try to explain the difference between ViewData, ViewBag and TempData. I hope you will
refer this article for your need. I would like to have feedback from my blog readers. Please post your
feedback, question, or comments about this article.

Print Article

Share this article with your friends!


in S h a r e 1

Tw eet

About the Author

Shailendra Chauhan works as Software Analyst at reputed MNC and has more than 5 years of hand
over Microsoft .NET technologies. He is a .NET Consultant and is the founder & chief editor of
www.dotnet-tricks.com and www.dotnetinterviewtricks.com blogs. He is an author of book ASP.NET
MVC Interview Questions and Answers.
He loves to work with web applications and mobile apps using Microsoft technology including ASP.NET,
MVC, C#, SQL Server, WCF, Web API, Entity Framework,Cloud Computing, Windows Azure, jQuery,
jQuery Mobile, Knockout.js, Angular.js and many more web technologies. More...

http://www.dotnet-tricks.com/Tutorial/mvc/9KHW190712-ViewData-vs-ViewBag-vs-TempData-vs-Session.html 3/3

You might also like