Wednesday, October 15, 2014

Life Cycle of TempData in MVC4

Life Cycle of TempData in MVC4





The idea behind to write this article came from after looking at the “such” image on many web portal as depicted below.



I have referred an image from one of dotnet community website.

Point1. clip_image003


If you notice above shown image I sense some wrongdoings with this because it shows that we could transfer the data from controller to view using VIewBag and ViewData. Though I have noticed we could also transfer data from Controller to View using TempData .Than an above image will look like shown below. I think we should proceed and see how?




I just run an application, it calls action method “verify”. Here we set some value to TempData

  1: TempData["EmployeeRegistration"] = ObjEmp.GetEmpRegistrationsDetails(); 

as shown in image below also:


Now I access the TempData value at View level very easily like shown in image below:



So it means that TempData value persist at successive request and can transmit from Controller to View. After transferring TempData value from controller to View ,if you again try to use it at other level than it will lost its value and become null. TempData is used to pass data from current request to subsequent request from one level to another level e.g. controller to view, controller to action to another action.



If you want retain TempData value for all request than just use the below method.




Redirection of TempData from one action to another action .please follow the given below steps:



It calls and action method “details” which takes one parameter “id” and also retain its value without using keep method. which means TempData is used to pass data from current request to subsequent request.



Kindly find an attached sample and try it once.

Disclaimer: These all are some finding which I have shared with you.Please touch base with me if you feel any query or suggestion of improvement.

I wish it will help you utilize both feature at best.

To learn more about MVC please go to the following link.

MVC Articles


Enjoy Coding and Readingclip_image017


Post a Comment