ASP.NET MVC 3 RC - Razor“视图”财产

发布于 2024-10-01 16:01:12 字数 803 浏览 5 评论 0原文

只是在今天发布的 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>

</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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

儭儭莪哋寶赑 2024-10-08 16:01:12

View 属性是 ViewData 属性的别名。这意味着以下代码

View.Title

等效于

ViewData["Title"]

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 the ViewData property. That means that the following code

View.Title

is equivalent to

ViewData["Title"]

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 call RenderBody.

月牙弯弯 2024-10-08 16:01:12

是的,“View”确实是 ViewData 并且他们使用 dynamic 以便拥有您所看到的语法 (View.Title)

它可以翻译为

ViewData["Title"]

在 MVC 中,特别是在 MVC 3 中,有很多像这样的小块,这会让您感到困惑:)。

Yes, "View" is really ViewData and they're using dynamic so as to have the syntax you're seeing (View.Title)

It translates to

ViewData["Title"]

Lots of little nuggets like this in MVC and specially in MVC 3 that will confuse you :).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文