ASP. TempData is used to transfer some temporary data, such as passing temporary data between controller actions or passing some temporary data to the view, I believe everyone has seen "What are the methods of passing values between ASP." This interview question, TempData is one of the methods of passing values in ASP. TempData By default is to use the session to store temporary data, the data stored in the TempData is only valid once access, once the end of the visit will be deleted. This one-time visit refers to a request to the next request, because after the next request arrives, the TempData data that is stored in the session is fetched and assigned to TempData, and then the data is removed from the session. Let's look at the ASP. NET MVC Preview5 source code:
That is, TempData is saved only to the next request, and after the next request is finished, the TempData is deleted. Note that here TempData uses session to do the storage, the session is corresponding to the specific user, so there is no concurrency problem. If you use a database to do tempdata storage media, you have to consider this situation. As for how to customize TempData storage media, you can refer to "ASP. NET MVC: Using db4o to do Tempdataprovider (with a generic Redirecttoaction method)" this article.
As mentioned earlier in our Basecontroller there is a way to display the message to the user, this message is temporary information, we can use TempData to achieve. Here's how to implement this tip:
protected ActionResult showmsg (list<string> msgs)
{
tempdata["Messages"] = msgs;
Return redirecttoaction ("Message");
}
Public ActionResult Message ()
{
Return View (tempdata["Messages"] as list<string>);
}
Because our controller is inherited from our custom basecontroller, so I can be the controller in this way to display the message to the user:
Well, this is the first part. enjoy! Post by Q.lee.lulu.
The code for the example blog in this article: 4mvcblog_5.rar
Go Introduction to ASP. NET MVC 6, TempData