ViewBag, ViewData and TempData

后端 未结 8 1872
半阙折子戏
半阙折子戏 2020-11-22 04:43

Could any body explain, when to use

  1. TempData
  2. ViewBag
  3. ViewData

I have a requirement, where I need to set a value in a control

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 05:33

    TempData will be always available until first read, once you read it its not available any more can be useful to pass quick message also to view that will be gone after first read. ViewBag Its more useful when passing quickly piece of data to the view, normally you should pass all data to the view through model , but there is cases when you model coming direct from class that is map into database like entity framework in that case you don't what to change you model to pass a new piece of data, you can stick that into the viewbag ViewData is just indexed version of ViewBag and was used before MVC3

提交回复
热议问题