In ASP.NET MVC there are three ways - ViewData, ViewBag, and TempData - to pass data from the controller to view the next request. Like WebForm, you can also use Session to persist data during a user session. Now, the question is when to use ViewData, VieBag, TempData, and Session. Each of them has their own importance. In this article, I am trying to explain the differences among these four.
ViewData
- ViewData is derived from the ViewDataDictionary class and is basically a Dictionary object i.e., Keys and Values where Keys are String while Values will be objects.
- Data is stored as Object in ViewData.
- While retrieving the data it needs to be Type Cast to its original type as the data is stored as the object and it also requires NULL checks while retrieving.
- ViewData is used for passing value from Controller to View.
- ViewData is available only for Current Request. It will be destroyed on redirection.
Example
In the below example, a string value is set in the ViewData object in Controller and it is then displayed in View.
Controller
- public class FirstController: Controller {
-
- public ActionResult Index() {
- ViewData["Message"] = "Hello RAJESH!";
- return View();
- }
- }
View
- <html>
-
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Index</title>
- </head>
-
- <body>
- <div> @ViewData["Message"] </div>
- </body>
-
- </html>
ViewBag
- ViewBag is a Wrapper built around ViewData.
- ViewBag is a dynamic property and it makes use of the C# 4.0 dynamic features.
- While retrieving, there is no need for Type Casting data.
- ViewBag is used for passing value from Controller to View.
- ViewBag is available only for Current Request. It will be destroyed on redirection.
Example
In the below example, a string value is set in the ViewBag object in Controller and it is then displayed in View.
Controller
- public class FirstController: Controller {
-
- public ActionResult Index() {
- ViewBag.Message = "Hello RAJESH!";
- return View();
- }
- }
View
- <html>
-
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Index</title>
- </head>
-
- <body>
- <div> @ViewBag.Message </div>
- </body>
-
- </html>
TempData
- TempData is derived from the TempDataDictionary class and is basically a Dictionary object i.e. Keys and Values where Keys are String while Values will be objects.
- Data is stored as Object in TempData.
- While retrieving the data it needs to be Type Cast to its original type as the data is stored as objects and it also requires NULL checks while retrieving.
- TempData can be used for passing value from Controller to View and also from Controller to Controller.
- TempData is available for Current and Subsequent Requests. It will not be destroyed on redirection.
Example
In the below example, a string value is a set of the TempData objects in Controller and they have redirected to another Controller and finally, it is displayed in View.
First Controller
- public class FirstController: Controller {
-
- public ActionResult Index() {
- TempData["Message"] = "Hello RAJESH!";
- return new RedirectResult(@ "~\Second\");
- }
- }
Second Controller
- <html>
-
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Index</title>
- </head>
-
- <body>
- <div> @TempData["Message"]; </div>
- </body>
-
- </html>
Session
- In ASP.NET MVC, Session is a property of Controller class whose type is HttpSessionStateBase.
public HttpSessionStateBase Session { get; }
- The Session is also used to pass data within the ASP.NET MVC application and Unlike TempData, it persists for its expiration time (by default session expiration time is 20 minutes but it can be increased).
- The Session is valid for all requests, not for a single redirect.
- It also requires typecasting for getting data and checks for null values to avoid error.
Thank you.