剃刀:@Html.Partial() 与 @RenderPage() [英] Razor: @Html.Partial() vs @RenderPage()

查看:27
本文介绍了剃刀:@Html.Partial() 与 @RenderPage()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

渲染子模板的适当方式是什么?

What is the appropriate way of rendering a child template?

那有什么区别?两者似乎都适合我.

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

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

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

推荐答案

Html.Partial("MyView")

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

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")

Html.Partial() 的作用相同,不同之处在于它将其输出直接写入响应流.这样效率更高,因为视图内容没有缓存在内存中.但是,由于该方法不返回任何输出,@Html.RenderPartial("MyView") 将不起作用.您必须将调用包装在代码块中:@{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")

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

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)

这篇关于剃刀:@Html.Partial() 与 @RenderPage()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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