当它使用权的ViewData代替的ViewModels? [英] When is it right to use ViewData instead of ViewModels?

查看:109
本文介绍了当它使用权的ViewData代替的ViewModels?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你想要让你使用一个ViewModel以包含在渲染视图中的数据来开发你的控制器,应该的所有的数据视图模型中包含的?这将是什么样的条件确定以绕过视图模型?

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 ViewModel? What conditions would it be ok to bypass the ViewModel?

我想问的原因是我在有些地方的code的使用ViewData的,有些是使用视图模型的位置。我想在球队分配了一套准则时,它的使用权的ViewData,当它只是走捷径。我想从谁处理了这个让我知道我的准则不只是我有偏见其他开发商的意见。

The reason I ask is I'm in a position where some of the code is using ViewData and some is using the ViewModel. I want to distribute a set of guidelines in the team on when it's right to use the ViewData, and when it's just taking shortcuts. I would like opinions from other developers who have dealt with this so that I know my guidelines aren't just me being biased.

推荐答案

只是为了进一步Fabian的评论;你可以明确地保证可视数据从未使用通过遵循的这篇文章。真的没有借口的的使用模型的一切。

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.

如果你没有选择,只能使用ViewData的(现有项目说了);至少是使用字符串常量来解析名称,以避免使用魔术字符串。沿着线的东西:计算机[ViewDataKeys.MyKey] = myvalue的; 逸岸,我用这为几乎任何需要基于字符串(会话密钥,缓存键,VaryByCustom是输出缓存键,等等)。

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).

这篇关于当它使用权的ViewData代替的ViewModels?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆