该策略旨在构建视图模型,构建要显示的模型并表示需要使用的数据。
例子:
你有这些类,类谁是你的数据库的表示:
public class FootballTeam{
public string Name{get;set;}
public string Logo{get;set;}
}
public class FootballGame{
public Datetime Date {get;set;}
public string Competition {get;set;}
}
public class Referee{
public string Name{get;set;}
public int Experience {get;set;}
}
要显示有关比赛的游戏的信息,您可以创建这个视图模型,类谁如果需要,可以参考您的商业模式的某些类别:
public class GameViewModel{
[DisplayName("Home team")]
public FootballTeam HomeTeam{get;set;}
[DisplayName("Referee")]
public Referee Referee{get;set;}
[DisplayName("Visitor team")]
public FootballTeam VisitorTeam {get;set;}
[DisplayName("Comments")]
public List<string> RedactionComments{get;set;}
}
并创建一个视图,该视图将使用此GameViewModel。 一般来说,当你创建一个新的MVC项目时,你的表现层中有一个名为“ViewModels”的文件夹,其中包含一些像这样的类。
此方法允许将您的业务模型与您的演示模型分开,这是两个完全不同的事情。
这里有非常好的答案:What is ViewModel in MVC?
您可以创建一个视图模型,构建为您的视图的表征的模型,谁可以包含一些楷模。 – AdrienTorris
http://stackoverflow.com/questions/20775802/creating-a-viewmodel-from-two-ef-models-in-asp-net-mvc5/20775904#20775904 – Shyju