ViewBag/ViewData Lifecycle

前端 未结 4 960
孤街浪徒
孤街浪徒 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:18

    ViewBag and ViewData are used for the same purpose. They are used to pass data from controllers to the View. When we assign any data or object to them they are accessible in the View.

    • ViewData: ViewData is a dictionary of objects and they are accessible by string as key.
    • ViewBag: Uses the dynamic feature. It allows an object to add dynamic properties to it.

提交回复
热议问题