ASP.NET MVC 3 - ViewModel最佳实践

时间:2022-09-18 08:15:29

I've a Razor view with lots of graph and other text fields which gets data from controller. I'm thinking of passing a ViewModel from controller to the view which then will parse the relevant content and display it.

我有一个Razor视图,其中包含大量图形和其他文本字段,可从控制器获取数据。我正在考虑将ViewModel从控制器传递到视图,然后该视图将解析相关内容并显示它。

Could anyone suggest if above approach is the best practice to solve such, in MVC?

任何人都可以建议在MVC中,如果上述方法是解决此类问题的最佳方法吗?

The ViewModel class may look like below:

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;}
   ......
}

Reason I'm trying to return ViewModel is that there are may parts of the page which have different data.

我试图返回ViewModel的原因是页面的某些部分可能有不同的数据。

2 个解决方案

#1


3  

Absolutely. A ViewModel is a perfectly acceptable solution to this problem. See Section 12.1.5 of Palermo's excellent MVC in Action book (conveniently in the free sample)

绝对。 ViewModel是解决此问题的完全可接受的解决方案。请参阅巴勒莫出色的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

#2


2  

I think you solution is corret.

我认为你的解决方案是正确的。

Another approach could be to split up the big razor view into smaller partial views, each with a simpler view model. This is usefull for readability, separation of responsability, ecc.

另一种方法可能是将大剃刀视图拆分为较小的局部视图,每个视图都具有更简单的视图模型。这对于可读性,责任分离,ecc非常有用。

#1


3  

Absolutely. A ViewModel is a perfectly acceptable solution to this problem. See Section 12.1.5 of Palermo's excellent MVC in Action book (conveniently in the free sample)

绝对。 ViewModel是解决此问题的完全可接受的解决方案。请参阅巴勒莫出色的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

#2


2  

I think you solution is corret.

我认为你的解决方案是正确的。

Another approach could be to split up the big razor view into smaller partial views, each with a simpler view model. This is usefull for readability, separation of responsability, ecc.

另一种方法可能是将大剃刀视图拆分为较小的局部视图,每个视图都具有更简单的视图模型。这对于可读性,责任分离,ecc非常有用。