Wednesday, October 15, 2014

Life Cycle of TempData in MVC4

Life Cycle of TempData in MVC4

 

 

Dotnetpiper

 

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

 

clip_image001

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?

 

clip_image004

 

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:

clip_image005

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

 

clip_image007

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 controller.one action to another action.

 

Point2.clip_image009

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

 

clip_image010

Point3.clip_image012

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

 

clip_image014

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.

 

clip_image016

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

Thanks

Enjoy Coding and Readingclip_image017

0 comments :

Post a Comment