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

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

问题描述

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

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

推荐答案

这是从<一个派生的aspx页面href=\"http://msdn.microsoft.com/en-us/library/dd470798.aspx\"><$c$c>System.Web.Mvc.ViewPage<TModel>.据说,这种看法是强类型的类型的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页面href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.viewpage.aspx\"><$c$c>System.Web.Mvc.ViewPage你需要从拉动值的ViewData 视图不再知道有关的TModel 键入

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

甚至更糟糕的:

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

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

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

另请注意,还有的<一个href=\"http://msdn.microsoft.com/en-us/library/dd505223.aspx\"><$c$c>ViewUserControl<TModel>对口强类型的谐音(ASCX)。

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

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

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