Differences between viewdata, viewbag, and tempdata in Asp.net MVC

Source: Internet
Author: User

1. viewdata and viewbag

The life cycle of viewbag and viewdata is the same and only valid for the current view. The difference is that viewbag is no longer a dictionary key-Value Pair structure, but a dynamic type.

 

2. tempdata

Tempdata is stored in the session. When the Controller executes a request, it first obtains tempdata from the session, then clears the session, and obtains tempdata. Although it is saved in an internal dictionary object, however, each entry in the set is deleted from the dictionary table once accessed.

DetailsCodeAt the level, the tempdata acquisition process reads data from the session of controllercontext through the sessionstatetempdataprovider. loadtempdata method, and then clears the session. Therefore, tempdata can only be transmitted once across the controller.

If you want to read the value of tempdata without deleting it, you can use the tempdata. Peek ("key") method. If you want to keep the value in tempdata again, you can use tempdata. Keep ("key ").

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.