加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 06:59:28 所属栏目:asp.Net 来源:网络整理
导读:我有一个Razor视图,有很多图形和其他文本字段,从控制器获取数据.我正在考虑将ViewModel从控制器传递到视图,然后该视图将解析相关内容并显示它. 任何人都可以建议,如果在MVC中,上述方法是解决此问题的最佳方法吗? ViewModel类可能如下所示: public class Vi
我有一个Razor视图,有很多图形和其他文本字段,从控制器获取数据.我正在考虑将ViewModel从控制器传递到视图,然后该视图将解析相关内容并显示它.

任何人都可以建议,如果在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是解决此问题的完全可接受的解决方案.参见巴勒莫出色的MVC行动书第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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读