视图模型显示的部分信息 [英] ViewModel to display partial information

查看:100
本文介绍了视图模型显示的部分信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我是新来的ASP.NET MVC和以学习被认为是良好做法的模式写一些code,我读了很多的论坛之前。和一个引起我注意的是视图模型。我明白,这是非常有帮助的,当我们想通过让我们说大概有两班的信息。要做到这一点的一个好方法是创建仅用于渲染其具有两个类的属性视图的信息类。但是,如果不是想从两个或多个域类显示信息,我想从一个单一的类显示的部分信息?例如用于登录查看用户的电子邮件是没有必要的。

So I'm new to the ASP.NET MVC and before writing some code I'm reading a lot of forums in order to learn patterns that are considered to be good practices. And one that caught my attention is the ViewModel. I understand that this is extremely helpful when we want to pass let's say information about two classes. A good way to do that is to create a class used only to render its information in the view having properties from both classes. But if instead of wanting to display information from two or more domain classes, I want to display partial information from a single class? For instance for a login view the user's email is not necessary at all.

因此​​,在这种情况下,它具有含用户只有部分数据的所有属性,然后多的ViewModels一类用户有意义或这是不正确的?

So in this case does it make sense having a class User with all the properties and then multiple ViewModels containing only partial data from User or is this incorrect?

推荐答案

是的,这是有道理的:

如果你的类有很多的属性,你只需要显示一对夫妇,我建议只查询你需要与这些属性创建一个视图模型的领域;这甚至会提高性能。

If your class has a lot of properties and you only need to display a couple, I'd recommend to only query the fields that you need and creating a ViewModel with those properties; this will even increase performance.

这篇关于视图模型显示的部分信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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