In ASP .NET MVC, ViewData, View Bag, TempData is used to maintain the state in our page/view.ย Viewdata, ViewBag is used to transfer date/information from controller to view in the current request. TempData use to transfer data from controller to another controller. It has capable of maintaining the data to not only on current request, it can maintain the data in next HTTP requests.
Flow Diagram :
ViewData
ViewData is a dictionary object, which used to maintain data from controller to view. It is derived from viewDataDictionary class. It requires typecasting for complex data and need to check null values to avoid error as shown below.
Note: The data inside the viewdata will be available in the current request only so the value becomeย NULLย if redirection occurs.
Example:
Controller
public ActionResult Index()
{
var dotnetproduct = new dotnetproduct(name: “MVC”);
ViewData[“dotnet-product”] = dotnetproduct;ViewData[“Name”] = “dotnet-helpers-ViewBag”;
return View();
}
View :ย
@ViewData[“Name”]
@{
var ViewDataproduct = ViewData[“dotnetproduct”] as dotnet-product; <!– Need to Typecast –>
if (@ViewDataproduct != null) <!– Need to check for NULL value –>
{
// Your Logic
}
}
ViewBag
ViewBag is a dynamic property (The ย properties that are associated with the dynamic are ignored at the compile time). And it has same purpose of viewdata for maintainย the data from controller toย view. It doesn’t require typecasting.
Note: The data inside the viewdata will be available in the current request only so the value becomeย NULLย if redirection occurs.
Example:
Controller
public ActionResult Index()
{
ViewBag.Name = “dotnet-helpers-ViewData”;
return View();
}
View :
@ViewData.Name
TempData
TempData is a dictionary derived from TempDataDictionary class. It differ from ViewBag and ViewData by it’sย life cycle of the object. TempData keeps the data up to next HTTP Request.
Note: The data inside the Tempdata will be available in the next request. In simple,ย itย helps to maintain data when weย move from one controller to other controller or from one action to other action. It requires typecasting for complex data type and check for null values to avoid error.
Controller
public ActionResult Index()
{
TempData[“Name”] = “dotnet-helpers-TempData”;
return RedirectToAction(“CheckTempData“);
}public ActionResult CheckTempData()
{
var model= TempData[“Name”];
return View(Name);
}
View :
@ViewData[“Name”]
Output : dotnet-helpers-TempData
Where to Use?
- ViewBag is a dynamic object, so we can add strongly typed objects, primitive values, etc that we need. We can choose ViewBag while passing dynamic data from view to controller.
- ViewData is a ViewDataDictionary, it can be accessed through a key (string).ย We can choose ViewDateย while passing data from view to controller.
- ViewData need typecasting while getting the original object/data.
- TempData is a dictionary derived from TempDataDictionary class. It can be used when we need to carry data from one controller to anotherย controller or one action to another action. It also requires typecasting while getting the original object/data.ย The data hadย removed when view reads them. For ย more detail about TempData click ย here
- ViewBag,ViewData can’t pass data back to controller.
Keep Cool Coding…
Leave A Comment