且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是强类型的ASP.NET MVC视图

更新时间:2022-11-23 13:50:24

这是从System.Web.Mvc.ViewPage<TModel>$c$c>.据说,这种看法是强类型的类型的TModel 。其结果是该有此视图内的型号财产这类型的的TModel ,并允许您直接访问模型的性质是这样的:

It is an aspx page that derives from System.Web.Mvc.ViewPage<TModel>. It is said that this view is strongly typed to the type TModel. As a consequence to this there's a Model property inside this view which is of type TModel and allows you to directly access properties of the model like this:

<%= Model.Name %>
<%= Model.Age %>

在这里仿佛从System.Web.Mvc.ViewPage$c$c>你需要从拉动值的ViewData ​​ code>视图不再知道有关的TModel 键入

<%= (string)ViewData["Name"] %>
<%= (int)ViewData["Age"] %>

甚至更糟糕的:

<%= ((SomeModelType)ViewData["model"]).Name %>

和有此类code无编译时的安全性。

and there's no compile time safety in such code.

另请注意,还有的ViewUserControl<TModel>$c$c>对口强类型的谐音(ASCX)。

Notice also that there's the ViewUserControl<TModel> counterpart for strongly typed partials (ASCX).