Introduction

Today, I am explaining about TempData vs ViewData vs Viewbag while working in MVC. Each one of the three (TempData, ViewData, ViewBag) terms are used to pass data from controller to View. These all have differing capacity to shield data while going from Controller to View using MVC application. In my recent articles, Explained thoroughly How to send SMS using C# and How to create charts using MVC

ViewBag

  1. ViewBag is used logically to pass the data from controller to view.
  2. It is being introduced since C# 4.0 .Net Framework.
  3. ViewData is property of ControllerBase Class from .Net Framework 4.5 C# first time.
  4. ViewBag life reach is short. It just lies under current requesting.
  5. If it redirects then its value becomes null.
  6. It doesn't required to typecasting for getting data from Controller to View.
  7. ViewBag is slower than ViewData.
//Controller Code snippet
public ActionResult Index()
{
      ViewBag.Tech = "Welcome to Technology Crowds";
      return View();
}

Razor View Code Snippet

    @ViewBag.Tech

ViewData

  1. ViewData is a property of ControllerBase Class.
  2. ViewData is derived from ViewDataDictionary Class.
  3. public ViewDataDictionary ViewData { get; set; }
    
  4. ViewData is used to pass data from Controller to View.
  5. It preserves the data only current request.
  6. Its value becomes null, if it redirects again.
  7. It is required strictly to typecast to avoid exception of null values.
  8. ViewData is faster than ViewBag.
TempData vs ViewData vs ViewBag

//Controller Code snippet
public ActionResult Index()
{
       ViewData["Tech"] = "Welcome to Technology Crowds"; 
      return View();
}

// Page View Code Snippet

@ViewData["Tech"]
    

TempData

  1. TempData is a property of ControllerBase Class.
  2. ViewData is derived from TempDataDictionary Class.
  3. public TempDataDictionary TempData { get; set; }
    
  4. TempData is main purpose to preserve data from one request to subsequent requests while passing data from one page to another page. 
  5. It is also required typecasting same as ViewData to avoid null exception.
//Controller Code snippet
 public ActionResult TempData_Sample()
{
    TempData["tc_msg"] = "Temp Data Sample...";
    return RedirectToAction("TempData_SampleView", "TempViewVsViewBagvsViewData");
}
public ActionResult TempData_SampleView()
{
   return View();
}
// Razor View Code Snippet
@TempData["tc_msg"]

Summary

I have cleared up about TempData vs ViewData vs ViewBag in complete significance, If I get any proposal from gathering of spectators then for the most part welcome. I think these three terms TempData vs ViewData vs ViewBag is all around endeavored to keep up state in MVC. Each one of the three can use as indicated by our asking for situation. These work immensely while we controlling data or running beginning with one controller then onto the following or View so these fulfils our essentials, in case we understand these absolutely, we'll not face any issue to control data between Controllers and Views in our MVC applications. If any idea with respect to these I'll recognize from my heart.

Download Working Sample Download

Relevant Reading