ViewBag vs ViewData vs TempData

In this article, we will learn about the differences between ViewBag vs ViewData vs TempData.

In ASP.NET MVC, ViewBag, View Data and TempData are used for passing information from the controller to view and in the next request.

ViewBag and ViewData are practically comparable and it assists us with moving the information from the controller to view through TempData likewise works during the current and resulting requests.

Let’s see the differences among these using code implementation.

What are the differences between ViewBag vs ViewData vs TempData?

What is ViewData?

To transfer data from Controller to View in MVC, we can use ViewData.
Every controller has a property called ‘ViewData’ of type ViewDataDictionary, which contains key-value pair.

A simple way to assign value to ViewData.

public ActionResult DisplayProductName()
            ViewData[“ProdName”] = “Laptop”; 
            return View();

Access ViewData in View as shown below.


Another Example of ViewData with sample code:

A Product model class.

public class Products
        public int ProductID { get; set; }
        public double ProductCost { get; set; }
        public string ProductName { get; set; }
        public string ProductDescription { get; set; }

A controller with ActionResult Index

public ActionResult Index()
            var prd = new Products() { ProductID = 1, ProductName = “MVC Ebook”, ProductCost = 500, ProductDescription = “An Ebook for MVC” };
            ViewData[“Product”] = prd;
            return View();

View index.cshtml

        var products =  (MVCInterview.Models.Products)ViewData[“Product”];

Output –

What is ViewBag?

It is similar to ViewData, but the difference is instead of using a string value, ViewBag uses a dynamic type. MVC 3.0 introduces ViewBag. ViewBag is a property of Controller class.

A sample code to assign value to ViewBag.

public ActionResult DisplayProductName()
            var product = new Products { ProductName = “Laptop” };
            ViewBag.Product = product;
            return View();

Access ViewBag in View –


What is TempData?

TempData is useful if you want to transfer data from one action to another action within the same controller or a different controller.

TempData in MVC is used to store temporary data for a short period of time.
What is the duration of a short period of time?
Here, a short period of time means a subsequent request. Data stored in TempData will be lost after the completion of the subsequent request.

Once you request – localhost/Test/Book then it will redirect to localhost/Test/DisplayBook and display the result – “MVC Ebook”.

public class TestController : Controller
        public ActionResult Book()
            TempData[“product”] = “MVC Ebook”;
            return RedirectToAction(“DisplayBook”);
        public string DisplayBook()
            return TempData[“product”].ToString();

But when you refresh the page again it will throw an error, see the below screenshot.

ViewBag vs ViewData vs TempData

It is a dynamic property for Controller class.ViewData is a ViewDataDictionary class object.TempData is a dictionary object derived from TempDataDictionary class.
Life span of ViewBag is only valid for the current request.To pass data from Controller to related View we use ViewData.TempData can pass data from one request to subsequent request.
In case of any URL redirection the ViewBag value gets null and void.In case of any URL redirection the ViewData value gets null.It requires type casting to avoid null error.
Syntax –
ViewBag.Count = 10
Syntax –
ViewData["Count"] = 10
Syntax –
TempData["Count"] = 10

Summary –

ViewData and ViewBag both are used to pass data from a Controller to a View. ViewData and ViewBag do not give compile time errors. Both ViewData and ViewBag are substantial just for the span of the current request. In the event that you set it in a controller activity and use it in the view, then it vanishes.

ViewBag is used to set and get value dynamically. ViewBag uses the dynamic property to store data.
ViewData is also used to store data similar to ViewBag. ViewData is of type ViewDataDictionary.
We can access ViewBag and ViewData in the corresponding View.
TempData is used to pass data from one action to another action, Action could be from the same controller or a different controller.

Previous blog on MVC

Please follow and like us:

Leave a Comment