asp.net-mvc 剃刀:@Html.Partial() 与 @RenderPage()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4501736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Razor: @Html.Partial() vs @RenderPage()
提问by Evgenyt
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.RenderPartial()
不再起作用?
回答by Annabelle
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).
将“MyView”视图呈现为MvcHtmlString
. 它遵循视图查找的标准规则(即检查当前目录,然后检查Shared
目录)。
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");}
.
与 相同Html.Partial()
,不同之处在于它将其输出直接写入响应流。这样效率更高,因为视图内容没有缓存在内存中。但是,由于该方法不返回任何输出,@Html.RenderPartial("MyView")
因此不起作用。你必须包装在一个代码块中的通话,而不是:@{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
将指定的视图(由路径和文件名而不是由视图名称标识)直接呈现到响应流,如Html.RenderPartial()
. 您可以将您喜欢的任何模型作为第二个参数包含在视图中
RenderPage("MyView.cshtml", MyModel)
回答by Ryan Sampson
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.
编辑:RenderPartial 的一个优点是您不必指定整个路径或文件扩展名,它会自动搜索公共位置。
回答by Omid Shariati
The RenderPartialmethod 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.
该的RenderPartial方法不返回HTML标记像大多数其他辅助方法。相反,它将内容直接写入响应流,这就是为什么我们必须像完整的 C# 行一样使用分号来调用它。
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.Partialmethod, which does exactly the same as the RenderPartialmethod, but returns an HTML fragment and can be used as @Html.Partial("Product", p).
这比从局部视图缓冲呈现的 HTML 稍微更有效,因为它无论如何都会被写入响应流。如果你喜欢更一致的语法,你可以使用Html.Partial方法,它与RenderPartial方法完全相同,但返回一个 HTML 片段,可以用作 @Html.Partial("Product", p)。
回答by bayram
We can also pass model using partial views. @Html.Partial("MyView","MyModel");
我们还可以使用局部视图传递模型。@Html.Partial("MyView","MyModel");
回答by Umar Aftab
@RenderPages()
The above does not work in ASP.NET MVC. It only works in WebPages.
以上在 ASP.NET MVC 中不起作用。它仅适用于网页。
@Html.Partial("_Footer")
You will need to use the above in ASP.NET MVC.
您将需要在 ASP.NET MVC 中使用上述内容。