While
working with ASP.NET MVC applications one need to retain data or need to transfer
data from controller to the view or one controller to another controller.
In ASP.NET
MVC, there are three types of objects - ViewBag, ViewData and TempData, use to
pass data from controller to view and controller to controller.
Each
object has its own functionality, importance and area in which it uses and one needs
to decide when to use ViewData, ViewBag or TempData. In this post we’ll look on
key points of these three objects.
ViewData: ViewData helps to maintain data
when you move from controller to view.
Example:
//In Controller defines ViewData FullName.
public ActionResult
Index()
{
ViewData["FullName"] = "Sandeep Kumar";
return View();
}
//In View use ViewData
@ViewData["FullName"]
|
Mentioned
below are some key points about ViewData:
·
ViewData
is used to pass data from controller to view.
·
ViewData
is a dictionary object and is of type ViewDataDictionary.
·
Just
like any other dictionary object in .NET, ViewData allows you to store key-value
pairs.
·
ViewData
is available for the current request only
·
ViewData
requires type casting for complex data type and it is recommended to check for
null values to avoid error.
·
If
redirection occurs, then its value becomes null.
·
ViewData
is introduced in MVC 1.0 and available in MVC 1.0 and above.
·
ViewData
is faster than ViewBag.
ViewBag: ViewBag also helps to maintain data
when you move from controller to view. It's a dynamic wrapper around view data.
Example:
//In Controller defines ViewBag FullName.
public ActionResult
Index()
{
return View();
}
//In View use ViewBag
@ViewBag.FullName
|
Mentioned
below are some key points about ViewBag:
·
ViewBag
is also used to pass data from the controller to the view.
·
ViewBag
is a dynamic wrapper around view data.
·
ViewBag
takes advantage of the new dynamic features in C# 4.0.
·
ViewBag
is also available for the current request only.
·
It
doesn’t require typecasting for complex data type.
·
If
redirection occurs, then its value becomes null.
·
ViewBag
is introduced in MVC 3.0 and available in MVC 3.0 and above.
·
ViewBag
is slower compare to ViewBag.
TempData: TempData helps to maintain data when you move from one controller to other
controller or from one action to other action. And it helps to maintain data between redirects.
Example:
public ActionResult
Index()
{
//Set Value in TempData
TempData["FullName"] = "Sandeep Kumar";
return RedirectToAction("UserDetail");
}
public ActionResult
UserDetail()
{
//Get value from TempData.
var name = TempData["FullName"];
return View(name);
}
|
Mentioned
below are some key points about ViewBag:
·
TempData
helps to maintain the data when we move from one controller to another
controller or from one action to another action
·
TempData
is a dictionary object and is of type TempDataDictionary.
·
Just
like ViewData, it also allows you to store key-value pairs.
·
TempData
is only work during the current and subsequent request.
·
TempData
requires type casting for complex data type and it is recommended to check for
null values to avoid error.
·
TempData
is used to pass data between two consecutive requests.
·
TempData
is introduced in MVC1.0 and available in MVC 1.0 and above.
·
TempData
internally uses session variables.
·
Though
TempData used for current and subsequent request but one can persist value in
TempData even after request completion. I’ve explained about this here.
Nice
ReplyDeletemain deference of viewbag and viewdata
ReplyDelete