Session Management in ASP.NET MVC
In this article, a developer discusses three different ways to deal with session management in ASP.NET MVC and provides some code for each.
Join the DZone community and get the full member experience.
Join For FreeDifference Between ViewData, ViewBag, and TempData With Code Examples
In this article, I will explain, with an example, the similarities and differences between ViewData
, ViewBag
, and TempData
in ASP.NET MVC.
ViewData
, ViewBag
and TempData
are used for transferring data and objects from the Controller to the View or from one Controller to another in ASP.NET MVC.
ViewData
ViewData
is derived from theViewDataDictionary
class and is basically a Dictionary object, i.e. it has keys and values where keys are strings and Values are objects.Data is stored as an Object in
ViewData
.While retrieving data, the data needs to be Type Cast to its original type, as it will be stored as an object.
ViewData
also requires NULL checks while retrieving data.ViewData
is used for passing a value from the Controller to the View.ViewData
is available only for Current Requests. It will be destroyed upon redirection.
Example
In the below example, a string value is set in the ViewData
object in the Controller and it is then displayed in the View.
Controller
public class FirstController: Controller {
// GET: First
public ActionResult Index() {
ViewData["Message"] = "Hello Sultan!";
return View();
}
}
View
<html>
<head>
<meta name="viewport" content="-width"/>
<title>Index</title>
</head>
<body>
<div>
@ViewData["Message"]
</div>
</body>
</html>
ViewBag
ViewBag
is a Wrapper built aroundViewData
.ViewBag
is a dynamic property and it makes use of C# 4.0's dynamic features.While retrieving data, there is no need for Type Casting the data.
ViewBag
is used for passing a value from the Controller to the View.ViewBag
is available only for Current Requests. It will be destroyed upon redirection.
Example
In the below example, a string value is set in the ViewBag
object in the Controller and it is then displayed in the View.
Controller
public class FirstController: Controller {
// GET: First
public ActionResult Index() {
ViewBag.Message = "Hello Sultan!";
return View();
}
}
View
<html>
<head>
<meta name="viewport" content="-width"/>
<title>Index</title>
</head>
<body>
<div>
@ViewBag.Message
</div>
</body>
</html>
TempData
TempData
is derived from theTempDataDictionary
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 an object in
TempData
.While retrieving, the data it needs to be Type Casted to its original type as the data is stored as objects and it also requires NULL checks while retrieving data.
TempData
can be used for passing a value from the Controller to the View and also from the Controller to another 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 set in the TempData
object in the Controller and it is redirected to another Controller and, finally, it is displayed in the View.
First Controller
public class FirstController: Controller {
// GET: First
public ActionResult Index() {
TempData["Message"] = "Hello Sultan!";
return new RedirectResult(@ "~\Second\");
}
}
Second Controller
public class SecondController: Controller {
// GET: Second
public ActionResult Index() {
return View();
}
}
View of Second Controller
<html>
<head>
<meta name="viewport" content="-width"/>
<title>Index</title>
</head>
<body>
<div>
@TempData["Message"];
</div>
</body>
</html>
Opinions expressed by DZone contributors are their own.
Comments