Monday 11 September 2017

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



Figure: Difference between tempdata, viewdata, and viewbag
·      Temp data - 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 - Helps to maintain data when you move from controller to view.
·      View Bag - It’s a dynamic wrapper around view data. When you use Viewbag type, casting is not required. It uses the dynamic keyword internally.
Figure: dynamic keyword
·      Session variables - By using session variables we can maintain data from any entity to any entity.
·      Hidden fields and HTML controls - 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.
Below is a summary table which 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

No comments:

Post a Comment

रूस-यूक्रेन संकट लाइव: भारतीयों को 'उपलब्ध किसी भी साधन' के माध्यम से कीव को तत्काल छोड़ने के लिए कहा

  रूस यूक्रेन संकट लाइव: कीव में भारतीय दूतावास ने मंगलवार को जारी एक एडवाइजरी में भारतीयों को  'किसी भी उपलब्ध साधन' के माध्यम से क...