When is it right to use ViewData instead of ViewModels?

后端 未结 4 1342
死守一世寂寞
死守一世寂寞 2021-01-11 18:28

Assuming you wanted to develop your Controllers so that you use a ViewModel to contain data for the Views you render, should all data be contained within the ViewMo

4条回答
  •  礼貌的吻别
    2021-01-11 19:03

    Just to further Fabian's comment; you can explicitly ensure viewdata is never used by following the steps outlined in this article. There's really no excuse not to use models for everything.

    If you have no choice but to use ViewData (say on an existing project); at the very least use string constants to resolve the names to avoid using 'magic strings'. Something along the lines of: ViewData[ViewDataKeys.MyKey] = myvalue; Infact, I use this for just about anything that needs to be "string-based" (Session Keys, Cache Keys, VaryByCustom output cache keys, etc).

提交回复
热议问题