What is the difference between viewdata and viewstate




















Just like the ViewData, the lifespan of the ViewBag start and ends in the current request. The ViewBag dynamic object is created and filled with data in the controller by adding a property you wish. The data is retrieved in the view using the same property name. The TempData object is derived from System. TempDataDictionary class. It is an un-typed key-value pair dictionary object.

The lifetime of TempData spans from one request to another. The data persists only when a call is redirected from one action method to another. The object will be automatically destroyed once it is used in the redirected action method. Normally TempData object is used to store and pass small amount of data like error message from one action method to another action method.

In case, if you want to keep the data in the TempData even after the first redirect, then you have to use the TempData. Keep method to retain the data for further redirection. TempData stores its contents in ASP. NET Session. So, you should careful while using TempData. Just like sessions, TempData will create problems while using it in webfarm cluster of servers environment.

ViewData is introduced in MVC 1. ViewBag is introduced in MVC 3. TempData is also introduced in MVC1. ViewBag only works with. TempData also works with. Type Conversion code is required while enumerating.

In depth, ViewBag is used dynamic, so there is no need to type conversion while enumerating. Its value becomes null if redirection has occurred.

TempData is used to pass data between two consecutive requests. TempData only works during the current and subsequent request. In ASP. Like WebForm, you can also use Session to persist data during a user session. Each of them has its own importance. In this article, I am trying to explain the differences among these four. ViewData is used to pass data from controller to corresponding view. ViewBag ViewBag is a dynamic property that takes advantage of the new dynamic features in C 4.

Basically it is a wrapper around the ViewData and also used to pass data from controller to corresponding view. View state is used only in ASP. View state is used by ASP. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. ViewState vs ViewData in mvc?

Ask Question. Asked 11 years, 2 months ago. Active 9 years, 1 month ago. Viewed 6k times. What is the difference between viewstate and viewdata in mvc? Improve this question.



0コメント

  • 1000 / 1000