ViewBag/ViewData Lifecycle

前端 未结 4 940
孤街浪徒
孤街浪徒 2020-12-15 02:54

I have seen many posts about when to use ViewBag/ViewData vs ViewModel but i have not been able to find an explanation of the lifecycle of the ViewBag.

For example,

4条回答
  •  囚心锁ツ
    2020-12-15 03:21

    The data you put in the ViewBag/ViewData is only available during the life-cycle of the request within which you populated it. MVC does not have post backs. If you need something to persist over more than a single request, you should use Session.

    Here is a decent article about the differences between ViewData, ViewBag, and TempData: http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications

提交回复
热议问题