Razor:@Html.Partial() 与 @RenderPage()
渲染子模板的正确方法是什么?
有什么区别呢?两者似乎都适合我。
为什么 @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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
将“MyView”视图呈现为
MvcHtmlString
。它遵循视图查找的标准规则(即检查当前目录,然后检查Shared
目录)。与
Html.Partial()
相同,只是它将输出直接写入响应流。这更有效,因为视图内容不会在内存中缓冲。但是,由于该方法不返回任何输出,因此@Html.RenderPartial("MyView")
将不起作用。您必须将调用包装在代码块中:@{Html.RenderPartial("MyView");}
。将指定视图(由路径和文件名而不是视图名称标识)直接渲染到响应流,如
Html.RenderPartial()
。您可以通过将其作为第二个参数包含在视图中来提供您喜欢的任何模型Renders the "MyView" view to an
MvcHtmlString
. It follows the standard rules for view lookup (i.e. check current directory, then check theShared
directory).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");}
.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我更喜欢
Over
Only,因为语法更简单并且更具可读性。除此之外,功能方面似乎没有任何差异。
编辑:RenderPartial 的优点之一是您不必指定整个路径或文件扩展名,它会自动搜索常见位置。
I prefer
Over
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 方法不像大多数其他帮助器方法那样返回 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).
我们还可以使用部分视图传递模型。 @Html.Partial("MyView","MyModel");
We can also pass model using partial views. @Html.Partial("MyView","MyModel");
上述内容在 ASP.NET MVC 中不起作用。它仅适用于网页。
您将需要在 ASP.NET MVC 中使用上述内容。
The above does not work in ASP.NET MVC. It only works in WebPages.
You will need to use the above in ASP.NET MVC.
对于 ASP.NET Core 7。在共享文件夹中创建部分文件,然后使用以下代码
For ASP.NET Core 7. In the Shared folder make partial file then user this following code
<partial name="_NavBar" />