ViewData vs ViewBag vs TempData vs Session in Asp .Net MVC

When working with ASP.NET MVC, there are several ways to pass data from the controller to the view. These include ViewData, ViewBag, TempData, and Session. In this blog post, we will discuss the differences between these four options and provide code examples of how to use them.

ViewData:

ViewData is a dictionary object that allows you to pass data from the controller to the view. It is a simple key-value pair, where the key is a string and the value is an object. The data stored in ViewData is only available for the current request and is not accessible in other requests.
//In the controller
ViewData["Name"] = "John Doe";

//In the view

Welcome, @ViewData["Name"]

In the above example, we are passing two pieces of data, the name and age of a person, to the view. The view can then access this data using the keys "name" and "age".

ViewBag:

ViewBag is a dynamic object that allows you to pass data from the controller to the view. It is similar to ViewData, but it does not require you to explicitly define the data type. The data stored in ViewBag is also only available for the current request and is not accessible in other requests.
//In the controller
ViewBag.Name = "Jane Smith";

//In the view

Welcome, @ViewBag.Name

In the above example, we are passing the same data as in the ViewData example, but we are using ViewBag instead. The view can access this data using the keys "name" and "age".

TempData:

TempData is a dictionary object that allows you to pass data from one request to another. It is similar to ViewData, but it persists across multiple requests. This means that the data stored in TempData can be accessed in other requests, not just the current one.
public ActionResult Index()
{
    TempData["name"] = "John Doe";
    TempData["age"] = 30;
    return RedirectToAction("About");
}
public ActionResult About()
{
    string name = TempData["name"] as string;
    int age = TempData["age"] as int;
    return View();
}
In the above example, we are passing data from the Index action to the About action using TempData. The About action can then access this data using the keys "name" and "age".

Session:

Session is a dictionary object that allows you to store data for a specific user. The data stored in session is available across multiple requests and can be accessed by the same user.
//In the controller
Session["Username"] = "johndoe";

//In the view

Welcome, @Session["Username"]

In the above example, we are storing data in the session for the current user. This data can be accessed in other actions and views for the same user.

Note: To use Session, you need to add the following line in the Startup.cs file:
services.AddSession();
and in the Configure method:
app.UseSession();


In conclusion, ViewData, ViewBag, TempData, and Session are all options for passing data from the controller to the view in ASP.NET MVC.

It is important to note that ViewData and ViewBag are not recommended for large amounts of data, due to the fact that the data is stored in the request, which can cause performance issues. While TempData and Session are best used for data that needs to be stored for a longer period of time.
 
About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice