InterviewSolution
| 1. |
Explain about TempData in ASP.NET MVC. |
|
Answer» TempData is a dictionary object useful for the temporary storage of data. It is an instance of the Controller base class. It is useful in storing the data for the duration of an HTTP request, or we can SAY that it can store live data between two consecutive REQUESTS of HTTP. It is also useful in passing the state between ACTION methods. TempData can be used only with the subsequent and current requests. It will make use of a session variable for storing the data. TempData needs typecasting during data retrieval. Example: In the below-given code, employee details will be TEMPORARILY stored by TempData. public ActionResult FirstRequest(){ LIST<string> TempDataDemo = new List<string>(); TempDataDemo.Add("Harish"); TempDataDemo.Add("Nagesh"); TempDataDemo.Add("Rakshith"); TempData["EmployeeName"] = TempDataDemo; return View();}public ActionResult ConsecutiveRequest(){ List<string> modelData = TempData["EmployeeName"] as List<string> ; TempData.Keep(); return View(modelData);} |
|