MVC ViewBag Best Practice

前端 未结 10 1891
野趣味
野趣味 2020-11-29 01:42

For the ViewBag, I heard it was a no-no to use. I would assume have the content from the ViewBag should be incorporated into a view model?

Question:

  1. <
10条回答
  •  無奈伤痛
    2020-11-29 02:07

    The issue with ViewBags and the recommended best practice boils down to compile time checking. ViewBags are just dictionaries and with that you get 'magic' strings, so if you end up changing the object type of one of the viewbag items or the key name you won't know until runtime, even if you precompile the views using true.

    Sticking to view models is best, even if you have to alter them to fit a specific view now and again.

提交回复
热议问题