gpt4 book ai didi

asp.net-mvc - ASP.NET MVC 3 - ViewModel 最佳实践

转载 作者:行者123 更新时间:2023-12-04 19:15:14 24 4
gpt4 key购买 nike

我有一个带有大量图形和其他文本字段的 Razor View ,它们从 Controller 获取数据。我想通过一个 ViewModel从 Controller 到 View ,然后将解析相关内容并显示它。

任何人都可以建议上述方法是否是解决此类问题的最佳实践,在 MVC 中?
ViewModel类可能如下所示:

public class ViewModelDemo
{
public MyChart chart {get;set;}
public string LeftContent {get;set}
public string bottomContent {get;set;}
public ChartLeged legent {get;set}
......
}

public class MyChart
{
public List<int> xAxis {get;set}
public List<int> yAxis {get;set;}
......
}

我想退货的原因 ViewModel是页面的某些部分可能具有不同的数据。

最佳答案

绝对地。 ViewModel 是这个问题的一个完全可以接受的解决方案。请参阅 Palermo 的优秀 MVC in Action 书的第 12.1.5 节(方便地在免费示例中)

The other option is to create a separate view model type for our views from the domain model. We’ll create a specialized class, just for that one view. We can shape that type however we like, and allow the view to shape our view model however we want. The advantage of a separated view model is that our views won’t influence the domain model in any way. For less complex applications, this separation is not necessary and overcomplicates the design. As complexity of the views increases, the design of the views has more and more impact on our domain model, unless the view model and domain model are separated.



http://www.manning.com/palermo/Samplechapter12.pdf

关于asp.net-mvc - ASP.NET MVC 3 - ViewModel 最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10552017/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com