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.

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.

Read Also:  Simple PHP Convert Date Format Examples

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.

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

Read Also:  Call Function in ng-click Event using Angular Example

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.

<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.

Read Also:  Create Dynamic Image Fancybox Gallery with jQuery, PHP & MySQL

How useful was this post?

Click on a star to rate it!

Free Live Chat for Any Issue