Pages

Men

rh

10/21/2014

Life Cycle of TempData in MVC4

The purpose of writing this article came after looking at the “such” image on many web portals as depicted below.

controller

I have referred to an image from one of the .Net community websites.

Point 1 point

Notice that in the image shown above I sense some wrongdoings with this because it shows that we could transfer the data from the controller to the view using VIewBag and ViewData. Though I have noticed we could also transfer data from a Controller to a View using TempData. Then the image above will look as shown below. I think we should proceed and see how.

Controller view

I just run an application, it calls the action method “verify”. Here we set some value to TempData TempData["EmployeeRegistration"] = ObjEmp.GetEmpRegistrationsDetails();

As shown in image below also:

return view

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

temp data

So it means that the TempData value persists at successive requests and can transmit from a Controller to a View. After transferring a TempData value from a controller to a View, if you again try to use it at another level then it will lose its value and become null. TempData is used to pass data from the current request to a subsequent request from one level to another level, for example a controller to a view, controller to controller or one action to another action.

Point 2point2

If you want to retain a TempData value for all requests then just use the following method.

employee registration


Point 3point3

Redirection of TempData from one action to another action. Please use the following procedure:

redirecttoaction

It calls an action method “details” that takes one parameter “id” and also retains its value without using the keep method. That means TempData is used to pass data from current request to a subsequent request.

check view data value

Kindly find an attached sample and try it once.

Disclaimer: These are all some findings that I have shared with you. Please touch base with me if you feel any query or suggestion for improving any of this.

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

Source collected from CSharpCorner.com
http://www.c-sharpcorner.com/UploadFile/97fc7a/life-cycle-of-tempdata-in-mvc4/

No comments :

Post a Comment