Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is TempData collection used for in asp.net MVC? [duplicate]

What is the actual use of TempData collection in asp.net MVC, I need pros and cons of that collection, and when do I need to use it, which views it is shared upon, or any useful information about it, finally if someone can tell me when to use it rather than ViewData?

Thanks in advance

CLOSED as exact duplicate of Difference Between ViewData and TempData?

like image 418
Amr Elsehemy Avatar asked Nov 24 '08 07:11

Amr Elsehemy


People also ask

When should we use TempData in MVC?

TempData is used to transfer data from the view to the controller, the controller to the view, or from an action method to another action method of the same or a different controller. TempData temporarily saves data and deletes it automatically after a value is recovered. Table of Content 1.

Does TempData pass data from one page to another page in MVC?

TempData is used to pass data from the current request to the next request.

What is the scope of TempData in MVC?

TempData is a dictionary object to pass the data from one action to other action in the same Controller or different Controllers. Usually, TempData object will be stored in a session object. Tempdata is also required to typecast and for null checking before reading data from it.

What is the difference between ViewData and TempData?

In one sentence: TempData are like ViewData with one difference: They only contain data between two successive requests, after that they are destroyed. You can use TempData to pass error messages or something similar.


1 Answers

TempData is used to share data between controller actions. If your controller does a RedirectToAction and the target action needs data (perhaps a particular model instance) to act upon, you can store this data in TempData. Using TempData is similar to storing it in the session, but only for one round-trip. You use TempData when you need to pass data to another controller action rather than a view for rendering.

like image 143
tvanfosson Avatar answered Sep 19 '22 12:09

tvanfosson