什么是 ASP.NET MVC 中的强类型视图 [英] What is strongly-typed View in ASP.NET MVC

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

问题描述

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

What is strongly-typed View in ASP.NET MVC?

推荐答案

它是一个源自 System.Web.Mvc.ViewPage.据说这个视图被强类型化为 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 %>

就好像您的 aspx 页面源自 System.Web.Mvc.ViewPage 您需要从 ViewData 中提取值,视图不再知道 TModel 类型:

where as if your aspx page derived from System.Web.Mvc.ViewPage you would need to pull values from ViewData the view no longer knows about the TModel type:

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

甚至更糟:

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

并且在这样的代码中没有编译时安全.

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

另请注意,还有 ViewUserControl 强类型部分 (ASCX) 的对应物.

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

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

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