ASP.NET MVC3,为什么我们需要强类型的视图? [英] ASP.NET MVC3 , Why we need strongly-typed View?

查看:264
本文介绍了ASP.NET MVC3,为什么我们需要强类型的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过斯科特Guthrie的博客 - <一个href=\"http://weblogs.asp.net/scottgu/archive/2010/10/19/asp-net-mvc-3-new-model-directive-support-in-razor.aspx\"相对=nofollow> ASP.NET MVC 3:在剃刀新@model关键字

一件事我不认识一个页面将结合方式不同的价值,但我们为什么要强制将视图模型绑定?

例如,论坛网站的用户控制面板,它可能有用户信息,职位历史,用户设置等。

从数据模型的角度来看,绑定源可以来自不同的表:用户,文章,user_settings等

但是,一个视图只能引用一个@model指令。

其实,我可以添加到模型什么属性我必须使用。
那么什么是make的优势视图成为强类型?


解决方案

  

但是,一个视图只能引用一个@model指令。


是的,这应该是您的视图模型。视图模型是你专门设计以满足给定观的要求一类。你为每个视图做到这一点。


  

从数据模型的视图,绑定源可以来自
  不同的表:用户,文章,user_settings等


大,然后设计将包含所有必要的属性,并有控制器构建这个视图模型汇集来自不同地方的信息,并将其传递给视图显示视图模型。

您永远不应该通过你的域模型,以你的观点。因为视图是一个或多个域模型通常投影=>因此需要定义视图模式。

I have read Scott Guthrie's Blog - ASP.NET MVC 3: New @model keyword in Razor

One things i don't realizes is a page will binding value in different ways,but why we have to enforce the view binding from Model?

For example, a user control panel of forum website, it may have the user information, the post history, the user setting etc.

From the view of data model, the binding source can be come from different table : users, posts, user_settings etc.

However, one view only can reference one @model directive.

Actually ,i can add what properties to model that i have to use. So what is the advantage of make the view became strongly-typed?

解决方案

However, one view only can reference one @model directive.

Yes, and this should be your view model. A view model is a class that you specifically design to meet the requirements of a given view. And you do this for each view

From the view of data model, the binding source can be come from different table : users, posts, user_settings etc.

Great, then design a view model that will contain all the necessary properties and have the controller build this view model aggregating the information from the different places and pass it to the view for displaying.

You should never pass your domain models to your views. Because views are normally the projection of one or more domain models => thus the need to define a view model.

这篇关于ASP.NET MVC3,为什么我们需要强类型的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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