ViewData VS ViewBag Vs TempData using ASP.Net MVC

ViewData VS ViewBag Vs TempData using ASP.Net MVC

Today, We want to share with you ViewData VS ViewBag Vs TempData using ASP.Net MVC.
In this post we will show you ViewData VS ViewBag Vs TempData in MVC , hear for What is ViewData, ViewBag and TempData? we will give you demo and example for implement.
In this post, we will learn about Difference Between ViewData, ViewBag and TempData with an example.

What is ViewData, ViewBag and TempData?


ViewBag is used to send a small amount of data to the view.

  Queue Job in Laravel 5.8 Tutorial

There may be some situation where you want to transfer some temporary data to view which is not included in the model object.

The viewBag is a dynamic type property of ControllerBase class.

It does not retain value when redirection occurs,it is available only for Current Request.

ViewBag is a dynamic property and it makes use of the C# 4.0 dynamic features.

<pre class="EnlighterJSRAW" data-enlighter-language="csharp">public class DemoController : Controller
    IList CarList = new List() {
    new Car(){ CarName="i10", CarColor="Black",CarMileage=18.2 },
    new Car(){ CarName="EON", CarColor="Silver", CarMileage=18.2 },
    new Car(){ CarName="Swift", CarColor="Red", CarMileage=18.2},
    new Car(){ CarName="Verna", CarColor="Black", CarMileage=18.2},
    new Car(){ CarName="Ciaz", CarColor="Silver", CarMileage=18.2}

<pre class="EnlighterJSRAW" data-enlighter-language="csharp">public ActionResult Index()
   ViewBag.TotalCar = CarList.Count();
   return View();


ViewData is similar to ViewBag, It can be used to send data from controller to view.

  Laravel User Role based Access control Authentication

It is a dictionary , which possese key-value pairs, where each key must be string.

Viewdata last long only for current http request.

<pre class="EnlighterJSRAW" data-enlighter-language="null">public class DemoController : Controller
    public ActionResult Index()
       ViewData["Simple"] = "This string is stored in viewData.";
       return View();

To get back value from viewData write below line in view.



TempData retain value across subsequent HTTP Request

It can be use to maintain data between controller actions as well as redirects.

It internally stores data in session but it get destroyed earliar than session.

In the below example, a string value is stored in the TempData object in JupiterController and it is redirected to EarthController and finally it is displayed in View.

  Google 1 share price today

<pre class="EnlighterJSRAW" data-enlighter-language="csharp">public class JupiterController : Controller
   // GET: First
   public ActionResult Index()
      TempData["Message"] = "Jupiter is largest planet in solar system";
      return new RedirectResult(@"~\Earth\");

public class EarthController : Controller
      // GET: Second
      public ActionResult Index()
         return View();

View of EarthController

<pre class="EnlighterJSRAW" data-enlighter-language="html">

   <title>Solar System</title>



Read :

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about ViewData, ViewBag and TempData in ASP.Net MVC.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Reply

Notify of