.Net Interview Questions and Answers

Search:

Confusing MVC Interview questions on TempData , Peek and Keep

By : Shiv Prasad Koirala | Sep 15th, 2014 | Views : 11462 | Comments : 0 Bookmark and Share

Introduction


One of the most asked MVC interview question is around tempdata , viewdata and viewbag. Further we have seen lot of MVC interviewers confuse developers with TempData , Peek and Keep questions. This section will clarify the confusion around the same.


What is need of "TempData" ?


TempData helps to preserve data throughout the current request. The request can travel through action to action or controller to controller until the view is displayed.


What is difference between TempData and ViewData ?


"TempData" maintains data for the complete request while "ViewData" maintains data only from Controller to the view.


Does "TempData" preserve data in the next request also?


"TempData" is available through out for the current request and in the subsequent request it's available depending on whether "TempData" is read or not.


So if "TempData" is once read it will not be available in the subsequent request.


What is the use of Keep and Peek in "TempData"?


Once "TempData" is read in the current request it's not available in the subsequent request. If we want "TempData" to be read and also available in the subsequent request then after reading we need to call "Keep" method as shown in the code below.


@TempData["MyData"];

TempData.Keep("MyData");


The more shortcut way of achieving the same is by using "Peek". This function helps to read as well advices MVC to maintain "TempData" for the subsequent request.


string str = TempData.Peek("Td").ToString();


If you want to read more in detail you can read from this detailed blog on MVC Peek and Keep.


If you want to Learn MVC from start below is the video link for the same.


 
Rate this Article :
1 Star
2 Stars
3 Stars
4 Stars
5 Stars
1 1 1 2 1
 
 
 
 

.NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala

In this article we will explain .NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala.... Read More

What is the difference between Stored Procedures and Function (SQL Server interview question with answers)?

In this article we will explain what is the difference between Stored Procedures and Function.For more articles and videos visit us on www.questpond.com... Read More

Explain MVC application life cycle?

In this article we will explain MVC application life cycle.For more articles and videos visit us on www.questpond.com... Read More

C# Design pattern interview questions with answers: - How Singleton is different from Static class?

In this article we will explain How Singleton is different from Static class.... Read More

WCF interview questions and answers: - What is the difference between Service endpoint and Client endpoint?

In this article we will explain What is the difference between Service endpoint and Client endpoint.... Read More

.NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala

In this article we will explain .NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala.... Read More

What is the difference between Stored Procedures and Function (SQL Server interview question with answers)?

In this article we will explain what is the difference between Stored Procedures and Function.For more articles and videos visit us on www.questpond.com... Read More

Explain MVC application life cycle?

In this article we will explain MVC application life cycle.For more articles and videos visit us on www.questpond.com... Read More

C# Design pattern interview questions with answers: - How Singleton is different from Static class?

In this article we will explain How Singleton is different from Static class.... Read More

WCF interview questions and answers: - What is the difference between Service endpoint and Client endpoint?

In this article we will explain What is the difference between Service endpoint and Client endpoint.... Read More

Article Categories

Learn in Hindi