Introduction:
In this article you will learn the following
1) What is
TempData and what is the use of TempData?
2) How to
pass data from Controller to controller or action to action?
3) How to
persist data in next request?
4) What is
Keep() and Peek() methods and their use?
In previous articles i explained the Use of viewbag, viewdata and tempdata in asp.net mvc and Pass data from controller to view in mvc and Difference between asp.net webforms and asp.net mvc and Dynamically bind mvc dropdownlist from sql server database using entity framework and MVC application to create, read, update, delete and search functionality usingrazor view engine and entity framework
What is
TempData and what is the use of TempData?
1) TempData
is like ViewData, except that it persists for two successive requests thus
helps to maintain data when we move from one controller to other controller or
from one action to other action. So TempData is used to pass data from one HTTP
request to next HTTP request.
2) TempData
is derived from TempDataDictionary and is a property of BaseController class.
3) Typecasting
for complex data type is required and it is recommended to check for null
values to avoid errors.
4) TempData
uses session to store the data, behind the scenes.
5) It
allows for persisting information for the duration of a single subsequent
request. We store something inside TempData and then redirect. In the target
controller action to which you redirected we can retrieve the value that was
stored inside TempData.
6) It
is mainly used to store only one time messages like error messages, validation
messages etc.
7) TempData
is meant to be a very short-lived instance thus lifetime of TempData is very
short i.e. from the current request to the subsequent request.
8) Use
TempData when you need data to be available for the next request only.
How to
pass data from Controller to controller or action to action?
Example
demonstrating the use of TempData
Right
click on Models folder -> Add -> Class and name it
"BookDetails.cs" and write the code as shown in image below:
Now create
the controller and add the code in the controller to store the data in the
TempData as shown in image below:
In above
example, we stored book object in TempData in Controller’s Index Action and
retrieved that in Controller’s another Action "DisplayBookData" that will
render the Book Details. Do you think it can be done using ViewBag or ViewData?
No actually because if we try to do the same using ViewBag or ViewData, we will
get null in Controller’s action "DisplayBookData" because only TempData object
maintains data between controller actions.
Now create
two view corresponding to Index() and DisplayBookData() actions defined in
HomeController. There is no need to write any code in the index.cshtml view
because that will not render any details instead we write the code on
DisplayBookData.cshtml view as shown in image below:
Now run
the application. Output will be as shown in image below:
How to
persist data in next request?
TempData’s
behavior (i.e. whether it persists/preserves values for the next request or
not) will be based on following 4 cases.
Case 1:
Not Read in First Request.
Case 2:
Read value in First Request.
Case 3:
Read & persist value using Keep.
Case 4:
Persist value using Peek and Read.
Let
us discuss these four cases one by one in detail.
Not Read
in First Request
If we set a value in TempData(e.g.
TempData["BookName"]="Ramayana") in our controller’s action but don’t read that
in view then TempData value will be persisted for the next request and will be available in next request.
Read in First Request
If we read TempData value as mentioned below in the
current request then its value will not be persisted for the next request i.e.
its value will be null on accessing in next request.
String bookName =TempData["BookName"].ToString()
Read & Persist using Keep
If we read TempData value in the current
request and we use "keep" method as mentioned below then value will be persisted
in TempData for the next request.
String bookName
= TempData["BookName"].ToString()
TempData.Keep();
Note:
Simple version of Keep method preservers all TempData objects for next request.
But if we want to preserve any specific object then we can use overloaded
version of Keep method where we can pass the Object name e.g. BookName here in
our example as following.
TempData.Keep("BookName");
Persist
using Peek and Read
If we read TempData by using the "Peek" method, then the
value will be persisted for the next request. This way we can read and retain
the value of TempData in single statement as following.
String
bookName = TempData.Peek("BookName").ToString();
Note:
There is no overload of Peek method.
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates.
If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..