asp.net-mvc Html.Partial 或 Html.RenderPartial 与 MVC3?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7371348/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:28:33  来源:igfitidea点击:

Html.Partial or Html.RenderPartial with MVC3?

asp.net-mvcasp.net-mvc-3

提问by Rubin

I am totally confused even after seeing the following explanation.

即使看到以下解释,我也完全困惑。

<div>
  @Html.Partial("_FeaturedProduct")
</div>

Partial views can be rendered inside a Layout Page (or if using MVC 2/3 w/ASPX, the Master Page) as well as regular views.

部分视图可以在布局页面(或者如果使用 MVC 2/3 w/ASPX,母版页)以及常规视图中呈现。

There are some cases where you might like to step aside and write directly to the HTTP Response stream rather than having a partial view render the results (partials/views use MvcHtmlString/StringWriter). To do so, use the Html.RenderPartial helper.

在某些情况下,您可能希望放弃并直接写入 HTTP 响应流,而不是让部分视图呈现结果(部分/视图使用 MvcHtmlString/StringWriter)。为此,请使用 Html.RenderPartial 帮助程序。

<div>
  @Html.RenderPartial("_FeaturedProduct")
</div>

Can someone tell me what it means? What cases where I might like to write direct to the HTTP Response etc. What if my partial view contains just one line like this:

有人能告诉我这是什么意思吗?在哪些情况下我可能想直接写入 HTTP 响应等。如果我的部分视图只包含这样的一行怎么办:

<h1>Hello</h1>

Which should I use and why? What would happen if I used the other?

我应该使用哪个,为什么?如果我使用另一个会发生什么?

The following confused me even more: "Use Html.RenderPartial for streaming images or other elements that are media-centric or where faster download times are highly important."

下面的内容让我更加困惑:“使用 Html.RenderPartial 来流式传输图像或其他以媒体为中心的元素或更快的下载时间非常重要的元素。”

采纳答案by LiamB

See the response below.

请参阅下面的回复。

The only difference is that Partial returns an MvcHtmlString, and must be called inside <%= %>, whereas RenderPartial returnsvoid and renders directly to the view.

If you look at the source code, you'll see that they both call the same internal method, passing a StringWriter for it to render to.

You would call Partial if you want to view, save, or manipulate the generated HTML instead of writing it to the page.

唯一的区别是 Partial 返回一个 MvcHtmlString,并且必须在 <%= %> 内部调用,而 RenderPartial 返回void 并直接渲染到视图。

如果您查看源代码,您会看到它们都调用相同的内部方法,传递一个 StringWriter 以供渲染。

如果您想查看、保存或操作生成的 HTML 而不是将其写入页面,则可以调用 Partial。

What is the difference (if any) between Html.Partial(view, model) and Html.RenderPartial(view,model) in MVC2?

MVC2 中的 Html.Partial(view, model) 和 Html.RenderPartial(view,model) 之间有什么区别(如果有的话)?