Lokesh Darapureddy: TempData vs ViewData            
       
                                                               

Friday, 28 June 2024

                               
                                                                   

                            TempData vs ViewData                        

                                                               
                       
ASP.NET Core MVC Data Transfer

Understanding ViewData, ViewBag, and TempData in ASP.NET Core MVC

Introduction

In ASP.NET Core MVC, ViewData, ViewBag, and TempData are used to pass data from controllers to views. Each has its own use case and characteristics.

ViewData

Type: ViewData is a dictionary of objects that is derived from ViewDataDictionary.
Usage: It is used to pass data from the controller to the view. Data passed using ViewData is available only for the current request.
Access: Data can be accessed using string keys.

// Setting ViewData in Controller
public IActionResult Index() {
    ViewData["Message"] = "Hello from ViewData";
    return View();
}

// Accessing ViewData in View
<h1>@ViewData["Message"]</h1>

ViewBag

Type: ViewBag is a dynamic property that provides a dynamic wrapper around ViewData. It uses the ExpandoObject class under the hood.
Usage: It is used to pass data from the controller to the view. Like ViewData, data passed using ViewBag is available only for the current request.
Access: Data can be accessed using dynamic properties.

// Setting ViewBag in Controller
public IActionResult Index() {
    ViewBag.Message = "Hello from ViewBag";
    return View();
}

// Accessing ViewBag in View
<h1>@ViewBag.Message</h1>

TempData

Type: TempData is a dictionary of objects derived from TempDataDictionary.
Usage: It is used to pass data from one request to another, such as from one action to another. It is particularly useful for passing data during redirects.
Access: Data can be accessed using string keys. TempData persists data until it is read. Once read, it is removed. To keep data for another request, use TempData.Keep().

// Setting TempData in Controller
public IActionResult Index() {
    TempData["Message"] = "Hello from TempData";
    return RedirectToAction("About");
}

// Accessing TempData in Another Action
public IActionResult About() {
    var message = TempData["Message"];
    ViewBag.Message = message;
    return View();
}

// Accessing TempData in View
<h1>@ViewBag.Message</h1>

Summary Table

Feature ViewData ViewBag TempData
Type Dictionary (ViewDataDictionary) Dynamic property (ExpandoObject) Dictionary (TempDataDictionary)
Scope Current request Current request Until read (persists across requests)
Usage Pass data from controller to view Pass data from controller to view Pass data between actions or across redirects
Access String keys Dynamic properties String keys
Example (Controller) ViewData["Message"] = "Hello"; ViewBag.Message = "Hello"; TempData["Message"] = "Hello";
Example (View) @ViewData["Message"] @ViewBag.Message @TempData["Message"] (in subsequent view)

Use Cases

ViewData: Use when you prefer dictionary-style syntax and need to pass data from the controller to the view for the current request.
ViewBag: Use when you prefer dynamic property syntax and need to pass data from the controller to the view for the current request.
TempData: Use when you need to pass data between actions or across redirects, persisting data until it is read.

                   
                                   
               
                                           

0 Comments:

                       
                                                                                                           
                       

                           

Post a Comment

                       

                       

Subscribe to Post Comments [Atom]

                                       

                        << Home