Razor:@Html.Partial() 与 @RenderPage()

发布于 2024-10-08 15:54:33 字数 100 浏览 5 评论 0原文

渲染子模板的正确方法是什么?

有什么区别呢?两者似乎都适合我。

为什么 @Html.RenderPartial() 不再起作用?

What is the appropriate way of rendering a child template?

And what's the difference? Both seem to work for me.

And why does @Html.RenderPartial() no longer work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

雾里花 2024-10-15 15:54:33
Html.Partial("MyView")

将“MyView”视图呈现为 MvcHtmlString。它遵循视图查找的标准规则(即检查当前目录,然后检查Shared 目录)。

Html.RenderPartial("MyView")

Html.Partial() 相同,只是它将输出直接写入响应流。这更有效,因为视图内容不会在内存中缓冲。但是,由于该方法不返回任何输出,因此 @Html.RenderPartial("MyView") 将不起作用。您必须将调用包装在代码块中:@{Html.RenderPartial("MyView");}

RenderPage("MyView.cshtml")

将指定视图(由路径和文件名而不是视图名称标识)直接渲染到响应流,如 Html.RenderPartial()。您可以通过将其作为第二个参数包含在视图中来提供您喜欢的任何模型

RenderPage("MyView.cshtml", MyModel)
Html.Partial("MyView")

Renders the "MyView" view to an MvcHtmlString. It follows the standard rules for view lookup (i.e. check current directory, then check the Shared directory).

Html.RenderPartial("MyView")

Does the same as Html.Partial(), except that it writes its output directly to the response stream. This is more efficient, because the view content is not buffered in memory. However, because the method does not return any output, @Html.RenderPartial("MyView") won't work. You have to wrap the call in a code block instead: @{Html.RenderPartial("MyView");}.

RenderPage("MyView.cshtml")

Renders the specified view (identified by path and file name rather than by view name) directly to the response stream, like Html.RenderPartial(). You can supply any model you like to the view by including it as a second parameter

RenderPage("MyView.cshtml", MyModel)
吾家有女初长成 2024-10-15 15:54:33

我更喜欢

@RenderPage("_LayoutHeader.cshtml")

Over

@{ Html.RenderPartial("_LayoutHeader"); }

Only,因为语法更简单并且更具可读性。除此之外,功能方面似乎没有任何差异。

编辑:RenderPartial 的优点之一是您不必指定整个路径或文件扩展名,它会自动搜索常见位置。

I prefer

@RenderPage("_LayoutHeader.cshtml")

Over

@{ Html.RenderPartial("_LayoutHeader"); }

Only because the syntax is easier and it is more readable. Other than that there doesn't seem to be any differences functionality wise.

EDIT: One advantage of RenderPartial is you don't have to specify the entire path or file extension it will search the common places automatically.

只为守护你 2024-10-15 15:54:33

RenderPartial 方法不像大多数其他帮助器方法那样返回 HTML 标记。相反,它写道
内容直接发送到响应流,这就是为什么我们必须像 C# 的完整行一样调用它,使用分号。

这比从部分视图缓冲渲染的 HTML 稍微高效一些,因为它将被写入
无论如何响应流。如果您喜欢更一致的语法,可以使用 Html.Partial 方法,该方法
RenderPartial 方法完全相同,但返回一个 HTML 片段,可用作
@Html.Partial(“产品”,p)。

The RenderPartial method doesn’t return HTML markup like most other helper methods. Instead, it writes
content directly to the response stream, which is why we must call it like a complete line of C#, using a semicolon.

This is slightly more efficient than buffering the rendered HTML from the partial view, since it will be written to the
response stream anyway. If you prefer a more consistent syntax, you can use the Html.Partial method, which
does exactly the same as the RenderPartial method, but returns an HTML fragment and can be used as
@Html.Partial("Product", p).

百善笑为先 2024-10-15 15:54:33

我们还可以使用部分视图传递模型。 @Html.Partial("MyView","MyModel");

We can also pass model using partial views. @Html.Partial("MyView","MyModel");

骄兵必败 2024-10-15 15:54:33
@RenderPages() 

上述内容在 ASP.NET MVC 中不起作用。它仅适用于网页。

@Html.Partial("_Footer")

您将需要在 ASP.NET MVC 中使用上述内容。

@RenderPages() 

The above does not work in ASP.NET MVC. It only works in WebPages.

@Html.Partial("_Footer")

You will need to use the above in ASP.NET MVC.

梦里人 2024-10-15 15:54:33

对于 ASP.NET Core 7。在共享文件夹中创建部分文件,然后使用以下代码

For ASP.NET Core 7. In the Shared folder make partial file then user this following code

<partial name="_NavBar" />

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