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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jignesh KumarPosted Dec 19, 2024, 3:43 AM
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 SharmaPosted Dec 18, 2024, 1:59 PM
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!!
Sangeetha SPosted Dec 18, 2024, 9:04 AM
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.
Jaish MathewsPosted Dec 18, 2024, 3:34 AM
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: