C# | Understanding ViewBag, ViewData, and TempData in ASP.NET MVC/ASP.NET Core🎯 🚀
In this article
You will get to know about...
→ ViewData
→ ViewBag
→ TempData
→ ViewBag
→ TempData
Introduction :
In ASP.NET MVC, 'ViewData', 'ViewBag', and 'TempData' are mechanisms used for passing data from the controller to the view, allowing the controller to send information that the view can then display. However, they differ in terms of scope, usage, and persistence.
ViewData : 🧾
Type : 'ViewData' is a dictionary-like object that stores data using key-value pairs.
Scope : It exists for the duration of a single request. It's used to pass data from the controller to the view.
Usage : You can use ViewData to pass data between a controller and a view. It's dynamic, so you need to use the as keyword to cast the data back to its original type in the view.
Scope : It exists for the duration of a single request. It's used to pass data from the controller to the view.
Usage : You can use ViewData to pass data between a controller and a view. It's dynamic, so you need to use the as keyword to cast the data back to its original type in the view.
// Controller
public IActionResult Index()
{
ViewData["Message"] = "Hello, ViewData!";
return View();
}
// View
@(ViewData["Message"] as string)
ViewBag : 🧳
Type : 'ViewBag' is a dynamic property that uses the ViewData dictionary under the hood.
Scope : It also exists for the duration of a single request. Like ViewData, it is used to pass data from the controller to the view.
Usage : It is a more convenient alternative to ViewData. It doesn't require casting in the view, and you can directly access properties.
Scope : It also exists for the duration of a single request. Like ViewData, it is used to pass data from the controller to the view.
Usage : It is a more convenient alternative to ViewData. It doesn't require casting in the view, and you can directly access properties.
// Controller
public IActionResult Index()
{
ViewBag.Message = "Hello, ViewBag!";
return View();
}
// View
@ViewBag.Message
TempData : 🔄
Type : 'TempData' is similar to ViewData but is specifically designed to persist data for an additional request (across redirects).
Scope : It persists data for the duration of the next request and is then automatically cleared. It's useful when you need to pass data between actions during a redirect.
Scope : It persists data for the duration of the next request and is then automatically cleared. It's useful when you need to pass data between actions during a redirect.
// Controller
public IActionResult Action1()
{
TempData["Message"] = "Hello, TempData!";
return RedirectToAction("Action2");
}
public IActionResult Action2()
{
// TempData["Message"] is still available here
return View();
}
// View
@TempData["Message"]
Keep in mind that while these mechanisms provide a way to pass data from the controller to the view, they should be used judiciously, and the choice between them often depends on the specific requirements of your application. Also, using strongly-typed models (via Model) is a recommended practice for passing data to views in a more type-safe manner.
0 Comments