ASP.NET MVC

What Is The Difference between Viewbag Viewdata and Tempdata?

Introductions

ViewBag, ViewData, and TempData all are objects in ASP.NET MVC and these are used to pass the data in various scenarios.

ViewData
ViewBag
TempData
It is Key-Value Dictionary collection It is a type object It is Key-Value Dictionary collection
ViewData is a dictionary object and it is property of ControllerBase class ViewBag is Dynamic property of ControllerBase class. TempData is a dictionary object and it is property of controllerBase class.
ViewData is Faster than ViewBag ViewBag is slower than ViewData NA
ViewData is introduced in MVC 1.0 and available in MVC 1.0 and above ViewBag is introduced in MVC 3.0 and available in MVC 3.0 and above TempData is also introduced in MVC1.0 and available in MVC 1.0 and above.
ViewData also works with .net framework 3.5 and above ViewBag only works with .net framework 4.0 and above TempData also works with .net framework 3.5 and above
Type Conversion code is required while enumerating In depth, ViewBag is used dynamic, so there is no need to type conversion while enumerating. Type Conversion code is required while enumerating
Its value becomes null if redirection has occurred. Same as ViewData TempData is used to pass data between two consecutive requests.
It lies only during the current request. Same as ViewData TempData only works during the current and subsequent request

ViewBag

1.ViewBag is a dynamic object to pass the data from Controller to View
2.it is restricted to the current request and the value of ViewData will become null while redirecting
3.ViewBag is slower than ViewData

eg.

Public ActionResult Index()
{
ViewBag.Title = “Welcome”;
return View();
}

ViewData

1.ViewData is a dictionary object to pass the data from Controller to View where data is passed in the form of key-value pair.
2. The scope of ViewData is similar to ViewBag and it is restricted to the current request and the value of ViewData will become null while redirecting
3.ViewData is Faster than ViewBag

eg.

Public ActionResult Index()  
{  
    ViewData[”Title”] = “Welcome”;  
    return View();  
}

 

TempData

1.TempData is a dictionary object to pass the data from one action to other action in the same Controller or different Controllers.
2.TempData will not null while redirecting.

eg.

Public ActionResult Index()  
{  
    TempData[”Data”] = “I am from Index action”;  
    return View();  
}  

Public string Get()  
{  
    return TempData[”Data”] ;  
}  

 

Bhavdip Talaviya

I'm an Asp.net MVC, Angular developer at Surat. I am an open-minded individual with a proven track record in designing websites and creating databases. I have strong technical skills as well as excellent interpersonal skills. I am eager to be challenged in order to grow and improve my communication and professional IT skills gained through previous experiences in the IT sector

Share
Published by
Bhavdip Talaviya

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

2 years ago