ASP.NET MVC 3 RC - Razor“视图”财产
只是在今天发布的 ASP.NET MVC 3 RC 中使用 Razor。
现在,我们有了“布局页面”的概念,我认为它是 ASPX 视图引擎中“View Master”的替代品。
但我不明白布局页面的“View”属性。
以下是创建新 Razor 视图时创建的示例:
_Layout.cshtml
<html>
<head>
<title>@View.Title</title>
...
MyView.cshtml
@model Mvc3FunParty.Models.Post
@{
View.Title = "Some Title";
Layout = "~/Views/Shared/_Layout.cshtml";
}
这会导致“Some Title”被插入到 <渲染的 HTML 的 code>
这到底是怎么运作的?当我将鼠标悬停在“View”属性上时,它的类型为“动态”。
那么这个属性到底应该用来做什么呢? 我们可以往里面塞东西吗?这应该是 ViewData 的 Razor 实现吗?
如果是这样,它不应该是“ViewDataDictionary”类型吗?为什么 View 属性采用“动态”类型?
Just mucking around with Razor in the ASP.NET MVC 3 RC released today.
Now, we have a concept of a "Layout Page", which i presume is the replacement of the "View Master" in the ASPX view engine.
But i do not understand the "View" property of the layout page.
Here is the example which is created when you create a new Razor View:
_Layout.cshtml
<html>
<head>
<title>@View.Title</title>
...
MyView.cshtml
@model Mvc3FunParty.Models.Post
@{
View.Title = "Some Title";
Layout = "~/Views/Shared/_Layout.cshtml";
}
Which results in "Some Title" being inserted into the <title>
tag of the rendered HTML.
How on earth does this work? When i hover over the "View" property, it's of type "dynamic".
So what exactly should this property be used for?
Can we stuff anything in there? Is this supposed to be the Razor implementation of ViewData?
And if so, shouldn't it be of type "ViewDataDictionary"? Why the "dynamic" type for the View property?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
View
属性是ViewData
属性的别名。这意味着以下代码等效于
This Works by using the new 'dynamic' feature of the C# language引入的.NET 4。基本上,它允许您使用迄今为止的静态类型语言编写后期绑定代码。如果您想了解更多信息,网络上有大量资源。
ViewData
属性仍然可用,并且您可以互换使用两者。它们都使用相同的后备存储,因此一种方式所做的更改将以另一种方式可用。使用
View
的优点是语法更简洁。缺点是您得不到 IntelliSense 支持。您可以在视图页面中设置
View.Title
并在布局页面中显示正确的值,这是由于 Razor 页面的渲染顺序所致。我们称之为由内而外的渲染,这意味着您的视图页面首先被执行,它的 HTML 输出被收集到缓冲区中,然后布局页面被执行,缓冲的视图页面输出被注入到您调用 RenderBody 的地方。The
View
property is an alias for theViewData
property. That means that the following codeis equivalent to
This works by using the new 'dynamic' feature of the C# language introduced in .NET 4. Basically it allows you to write late-bound code in what has until now been a statically-typed language. There's plenty of resources on the web if you want to learn more.
The
ViewData
property is still available and you can use both interchangaebly. They both use the same backing storage so changes made one way will be available the other way.The advantage of using
View
is more concise syntax. The disadvantage is that you do not get IntelliSense support.The reason why you can set
View.Title
in your view page and the correct value shows up in the layout page is due to the rendering order of Razor pages. We call it the inside-out rendering, which means that your view page gets executed first, it's HTML output gets collected into a buffer, then the layout page gets executed, and the buffered view page output gets injected where you callRenderBody
.是的,“View”确实是
ViewData
并且他们使用dynamic
以便拥有您所看到的语法 (View.Title
)它可以翻译为
在 MVC 中,特别是在 MVC 3 中,有很多像这样的小块,这会让您感到困惑:)。
Yes, "View" is really
ViewData
and they're usingdynamic
so as to have the syntax you're seeing (View.Title
)It translates to
Lots of little nuggets like this in MVC and specially in MVC 3 that will confuse you :).