What is the difference between tempdata, viewdata, and viewbag?

Lee Patric

Total Post:3

Points:21

Posted by  Lee Patric
 673  View(s)
Ratings:
Rate this:

Difference between tempdata, viewdata, and viewbag.

  1. Re: What is the difference between tempdata, viewdata, and viewbag?

    Temp data - It helps to maintain data when you move from one controller to another controller or from one action to another action. In other words when you redirect, tempdata helps to maintain data between those redirects. It internally uses session variables.

    View data - These helps to maintain data when you move from controller to view.

    View Bag - This is a dynamic wrapper around view data. When you use Viewbag type, casting is not required. It uses the dynamic keyword internally.

    Dynamic Keyword

    Session variables - With the help of session variables we can maintain data from any entity to any entity.

    Hidden fields and HTML controls - These helps to maintain data from UI to controller only. So you can send data from HTML controls or hidden fields to the controller using POST or GET HTTP methods.

    Here, below this is a summary table that shows the different mechanisms for persistence.

    Maintains data between
    ViewData/ViewBag
    TempData
    Hidden fields Session
    Controller to Controller
    No
    Yes
    No
    Yes
    Controller to View
    Yes
    No
    No
    Yes
    View to Controller No No Yes
    Yes





      Modified On Oct-15-2019 01:50:38 AM

Answer

Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.