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

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

问题描述

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

和有什么区别?似乎都为我工作。

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


解决方案

  Html.Partial(MyView的)

渲染MyView的以一个 MvcHtmlString 。它遵循视图查找标准规则(即检查当前目录,然后检查共享目录)。

  Html.RenderPartial(MyView的)

做同样的 Html.Partial(),除了它直接将其输出到响应流。这是更有效的,因为该视图内容不是在存储器缓冲。然而,由于该方法不返回任何输出, @ Html.RenderPartial(MyView的)将不起作用。你必须包装该呼叫并在code座,而不是: @ {Html.RenderPartial(MyView的);}

  RenderPage(MyView.cshtml)

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

  RenderPage(MyView.cshtml,为MyModel)

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?

解决方案

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)

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

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