Search This Blog

copyright@indiantechaware. Powered by Blogger.

ViewData vs TempDate | Asp.Net Core MVC tutorial

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

ViewData vs TempDate | Asp.Net Core MVC tutorial

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

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.

Name

ltr
item
GadgetGalli: ViewData vs TempDate | Asp.Net Core MVC tutorial
ViewData vs TempDate | Asp.Net Core MVC tutorial
GadgetGalli
https://gadgetgalli.blogspot.com/2024/06/viewdata-vs-tempdate-aspnet-core-mvc.html
https://gadgetgalli.blogspot.com/
https://gadgetgalli.blogspot.com/
https://gadgetgalli.blogspot.com/2024/06/viewdata-vs-tempdate-aspnet-core-mvc.html
true
2165627238268571711
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy