Hello every one I have one query what is this ViewData vs ViewBeg vs TempData .
Loading
Hello every one I have one query what is this ViewData vs ViewBeg vs TempData .
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jignesh KumarRank #91 · 22.9K reputationPosted Dec 18, 2024, 10:43 PM EST
Hello Sourabh,
Please find below article to get detailed explanation :
https://www.c-sharpcorner.com/blogs/main-difference-between-viewdata-vs-viewbag-vs-tempdata-in-asp-net-mvc
Vedio for in-depth explanation : https://www.youtube.com/watch?v=yq1N7zfZTBo
Onkar SharmaRank #104 · 20.6K reputationPosted Dec 18, 2024, 8:59 AM EST
In ASP.NET MVC, ViewData, ViewBag, and TempData are used to pass data from controllers to views, but they have different uses and behaviors. Here's a breakdown of each:
ViewData- Type: ViewData is a dictionary of type ViewDataDictionary.
- Purpose: Used to pass data from a controller to a view.
- Syntax: Accessed using keys, e.g., ViewData["Key"].
- Lifetime: Only available for the current request. Once the view is rendered, ViewData is disposed of.
- Features: Requires type casting and null checking since it uses a weakly typed dictionary.
ViewBag- Type: ViewBag is a dynamic property, a wrapper around ViewData.
- Purpose: Similar to ViewData, ViewBag passes data from controllers to views.
- Syntax: Uses dynamic properties, e.g., ViewBag.Key.
- Lifetime: Like ViewData, it's available only for the duration of the current request.
- Features: Easier to use than ViewData because it does not require type casting. However, it sacrifices compile-time checking because it's dynamic.
TempData- Type: TempData is also a dictionary of type TempDataDictionary.
- Purpose: Used for passing data between controller actions. Typically, this is used for redirection scenarios.
- Syntax: Accessed using keys, e.g., TempData["Key"].
- Lifetime: Its data persists for the duration of an HTTP request and survives one additional redirect. After that, it's cleared out.
- Features: Useful for transferring data between actions where the user is quickly redirected from one action to another.
Summary1. Lifetime:
2. Type Safety:
3. Usage:
Choosing between them depends on your specific needs concerning data lifetime and convenience with respect to type safety and ease of use.
To know more about: ViewBag, ViewData And TempData, visit:
Thanks!!
Jaish MathewsRank #225 · 11K reputationPosted Dec 17, 2024, 10:34 PM EST
Here is a tabular comparison of
ViewData,ViewBag, andTempDatain ASP.NET MVC:ViewDataDictionary)TempDataDictionary)ViewData["key"] = value;ViewBag.key = value;TempData["key"] = value;ViewData["Message"] = "Hello";ViewBag.Message = "Hello";TempData["Message"] = "Hello";Use the one most appropriate for your scenario:
Sangeetha SRank #212 · 11.4K reputationPosted Dec 18, 2024, 4:04 AM EST
TempData is used to pass data between requests, particularly useful for redirect scenarios.
ViewData and ViewBag are used for passing data to the view within the same request.