ViewData vs ViewBag vs TempData

ViewData, ViewBag and TempData in ASP.Net MVC are available to pass data from controller to view and so on. Let’s see the differences among these using code implementation.

What is ViewData?

ViewData in MVC is used to transfer data from Controller to View.
Every controller has a property called as ‘ViewData’ of type ViewDataDictionary, which contain 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 dynamic type.ViewBag was introduced in ASP.Net MVC 3.0;
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 different controller.

TempData in MVC is used to store temporary data for a short period of time.
What is the duration of short period time?
Here, short period of time means for a subsequent request. Data stored in TempData will be lost after 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 error, see below screen shot.

Summary –

ViewData and ViewBag both are used to pass data from a Controller to a View.
ViewBag and ViewData does not give compile time error.
ViewBag is used to set and get value dynamically. ViewBag uses dynamic property to store data.
ViewData is also use to store data similar to ViewBag. ViewData is of type ViewDataDictionary.
We can access ViewBag and ViewData in corresponding View.
TempData is used to pass data from one action to another action, Action could be from same controller or different controller.

Previous blog on MVC-

Please follow and like us:

Leave a Comment