Introduction to ASP. NET MVC 6, TempData

Source: Internet
Author: User

Original address: http://www.cnblogs.com/QLeelulu/archive/2008/10/04/1303948.html

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:

protectedactionresult showmsg (List<string>msgs)
{
tempdata["Messages"] =msgs;
returnRedirecttoaction ("Message");
}

PublicActionResult Message ()
{
returnView (tempdata["Messages"]  asList<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:


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.