Transfer of values between two actions in ASP. NET MVC--tempdata

Source: Internet
Author: User

I. tempdata in ASP. NET MVC

There is a property called TempData in the controllerbase of the ASP. Tempdatadictionary, which is a dictionary class as its name implies. The role of tempdata in ASP. NET MVC is that it can be used to pass values between action execution procedures. Simply put, you can store data in TempData when you execute an action, and you can use the data in TempData the next time the action is executed.

Such as:

1  Publicactionresult Index ()2 {3      This. tempdata["Mynane"] ="xiaoming";4     returnView ();5 }6  Publicactionresult Index2 ()7 {8      stringMyname= This. tempdata["Mynane"] as string;9      returnView ();Ten}

In the above Code, index () adds a key-value pair to tempdata, assuming that we first request the action of index and then request Index2 the action, then in INDEX2 we can get the key-value pair that we added to the tempdata. Interestingly, if INDEX2 is requested again, the value of MyName read from TempData will be null. So, we need to understand the life cycle of tempdata.

Two. The life cycle of the TempData

We know that HTTP is stateless, why TempData can pass data before two requests? Obviously, this data must have been saved in some form. Looking at the source code of the Controller class, it's easy to find what we want:

1 protected Override voidExecutecore ()2 {3 tempdata.load (ControllerContext, tempdataprovider);4     Try5     {6         stringActionName = routedata.getrequiredstring ("Action");7         if(!actioninvoker.invokeaction (ControllerContext, actionname))8         {9 handleunknownaction (actionname);Ten         } One     } A     Catch(Exception) -     { -  the Tempdata.save (ControllerContext, tempdataprovider); -     } -}

As you can see from the code above, you call the Tempdata.load () method every time you execute the action, and then call the Tempdata.save () method after you have finished executing the action. In addition, there is an important member of Tempdataprovider.

After reading the relevant source code, the truth comes out.

Tempdata.load ()

1  Public voidLoad (ControllerContext controllercontext, Itempdataprovider tempdataprovider)2 {3idictionary<string,Object> providerdictionary =Tempdataprovider.loadtempdata (controllercontext);4_data = (Providerdictionary! =NULL) ?Newdictionary<string,Object>(Providerdictionary, stringcomparer.ordinalignorecase):5         Newdictionary<string,Object>(stringcomparer.ordinalignorecase);6_initialkeys =Newhashset<string>(_data. Keys);7 __modifiedkeys.clear ();8}

Tempdata.save ()

1  Public voidSave (ControllerContext controllercontext, Itempdataprovider tempdataprovider)2 {3     if(_modifiedkeys.count >0)4     {5 6         //Apply change tracking.7         foreach(stringXinch_initialkeys)8         {9             if(!_modifiedkeys.contains (x))Ten             { One _data. Remove (x); A             } -         } -  the         //Store the dictionary - Tempdataprovider.savetempdata (ControllerContext, _data); - _modifiedkeys.clear (); -     } +}

The Tempdataprovider is used for staging data. In the Tempdata.load () method, the data saved in Tempdataprovider is read into tempdata for use during the action call. When the action is finished, Tempdata.save () removes any key-value pairs that are not updated in TempData and then saves the data in the TempData for the next call (note: That is, only the updated, And the newly added key-value pair to continue using in the next request.) Why does the data in TempData need to be purged quickly? Very simple, save memory.

Three. Itempdataprovider

The previously mentioned Tempdataprovider is a property of the controller, which is defined as follows:

1  PublicItempdataprovider Tempdataprovider2 {3     Get4     {5         if(_tempdataprovider==NULL){6_tempdataprovider =NewSessionstatetempdataprovider ();7         }8         return_tempdataprovider;9     }Ten     Set One     { A_tempdataprovider =value; -     } -}

Here we see a default implementation of the Sessionstatetempdataprovider class. That is, by default, ASP. NET MVC saves tempdata data by Sessionstatetempdataprovider. Obviously, the data is present in the session, that is, if you disable sessionstate, then your page is reported as abnormal.

ASP. NET MVC is inherently designed to be extensible, and we can easily replace this default Sessionstatetempdataprovider by implementing our own Itempdataprovider class. It is important to note that the data stored by Tempdataprovider must have user independence.

The Itempdataprovider interface definition is simple:

1   Public Interface Itempdataprovider 2         {3             idictionary<stringobject> loadtempdata ( ControllerContext controllercontext); 4             void Savetempdata (ControllerContext controllercontext, idictionary<stringobject>  values); 5         }

In Mvcfutures, you can also find a cookietempdataprovider that provides the implementation of storing tempdata in a cookie.

Transfer of values between two actions in ASP. NET MVC--tempdata

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.