Html.Partial vs Html.RenderPartial & Html.Action 与 Html.RenderAction
之间有什么区别
- 在 ASP.NET MVC 中,
Html.Partial
和Html.RenderPartial
Html.Action
和Html.RenderAction
In ASP.NET MVC, what is the difference between:
Html.Partial
andHtml.RenderPartial
Html.Action
andHtml.RenderAction
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
Html.Partial
返回一个字符串。Html.RenderPartial
在内部调用Write
并返回void
。基本用法是:
在上面的代码片段中,两个调用将产生相同的结果。
虽然可以将
Html.Partial
的输出存储在变量中或从方法返回它,但不能使用Html.RenderPartial
执行此操作。结果将在执行/评估期间写入
Response
流。这也适用于
Html.Action
和Html.RenderAction
。Html.Partial
returns a String.Html.RenderPartial
callsWrite
internally and returnsvoid
.The basic usage is:
In the snippet above, both calls will yield the same result.
While one can store the output of
Html.Partial
in a variable or return it from a method, one cannot do this withHtml.RenderPartial
.The result will be written to the
Response
stream during execution/evaluation.This also applies to
Html.Action
andHtml.RenderAction
.将 @Html.Partial 视为复制到父页面中的 HTML 代码。
将 @Html.RenderPartial 视为合并到父页面中的 .ascx 用户控件。 .ascx 用户控件的开销要大得多。
'@Html.Partial' 返回一个与父级内联构造的 html 编码字符串。它访问父级的模型。
'@Html.RenderPartial' 返回 .ascx 用户控件的等效项。它获取自己的页面 ViewDataDictionary 副本,并且对 RenderPartial 的 ViewData 所做的更改不会影响父级的 ViewData。
使用反射我们发现:
Think of @Html.Partial as HTML code copied into the parent page.
Think of @Html.RenderPartial as an .ascx user control incorporated into the parent page. An .ascx user control has far more overhead.
'@Html.Partial' returns a html encoded string that gets constructed inline with the parent. It accesses the parent's model.
'@Html.RenderPartial' returns the equivalent of a .ascx user control. It gets its own copy of the page's ViewDataDictionary and changes made to the RenderPartial's ViewData do not effect the parent's ViewData.
Using reflection we find:
这是我发现的:
当您没有要发送到视图的模型并且有大量不需要存储在变量中的 html 要带回时,请使用 RenderAction 。
当您没有要发送到视图的模型并且需要带回一些需要存储在变量中的文本时,请使用操作。
当您有一个模型要发送到视图并且有大量不需要存储在变量中的 html 时,请使用 RenderPartial。
当您有一个模型要发送到视图并且需要将一些文本存储在变量中时,请使用Partial。
RenderAction 和 RenderPartial 速度更快。
Here is what I have found:
Use RenderAction when you do not have a model to send to the view and have a lot of html to bring back that doesn't need to be stored in a variable.
Use Action when you do not have a model to send to the view and have a little bit of text to bring back that needs to be stored in a variable.
Use RenderPartial when you have a model to send to the view and there will be a lot of html that doesn't need to be stored in a variable.
Use Partial when you have a model to send to the view and there will be a little bit of text that needs to be stored in a variable.
RenderAction and RenderPartial are faster.
区别在于第一个返回
MvcHtmlString
,但第二个 (Render..
) 直接输出到响应。Difference is first one returns an
MvcHtmlString
but second (Render..
) outputs straight to the response.@Html.Partial
和@Html.RenderPartial
当您的 Partial 视图模型是父模型对应时使用,我们不需要创建任何操作方法来调用它。@Html.Action
和@Html.RenderAction
当您的部分视图模型独立于父模型时使用,基本上当您想要在上面显示任何小部件类型内容时使用页。您必须创建一个操作方法,该方法在从视图调用该方法时返回部分视图结果。@Html.Partial
and@Html.RenderPartial
are used when your Partial view model is correspondence of parent model, we don't need to create any action method to call this.@Html.Action
and@Html.RenderAction
are used when your partial view model are independent from parent model, basically it is used when you want to display any widget type content on page. You must create an action method which returns a partial view result while calling the method from view.根据我的说法,
@Html.RenderPartial()
的执行速度比@Html.Partial()
更快,因为 Html.RenderPartial 可以快速响应输出。因为当我使用
@Html.Partial()
时,与@Html.RenderPartial()
相比,我的网站需要更多时间来加载According to me
@Html.RenderPartial()
has faster execution than@Html.Partial()
due to Html.RenderPartial gives a quick response to Output.Because when I use
@Html.Partial()
, my website takes more time to load compared to@Html.RenderPartial()
关于这个问题的更多信息:
来自 Professional ASP.NET MVC 1.0 的“NerdDinner”
More about the question:
“NerdDinner” from Professional ASP.NET MVC 1.0
区别:
RenderPartial
的返回类型是void
,而Partial
返回MvcHtmlString< /code>
在 Razor 视图中调用
Partial()
和RenderPartial()
方法的语法<块引用>
@Html.Partial("PartialViewName")
@{ Html.RenderPartial("PartialViewName"); }
Partial()
和RenderPartial()
方法的语法以下是与
Partial()
和RenderPartial()
相关的 2 个常见面试问题什么时候会使用
Partial()
而不是RenderPartial()
,反之亦然?主要区别在于
RenderPartial()
返回 void,输出将直接写入输出流,而Partial()
方法返回MvcHtmlString,可以将其分配给变量并根据需要对其进行操作。因此,当需要将输出分配给变量以对其进行操作时,请使用 Partial(),否则使用 RenderPartial()。
哪一个性能更好?
从性能角度来看,直接渲染到输出流更好。
RenderPartial()
执行完全相同的操作,并且性能优于Partial()
。Differences:
The return type of
RenderPartial
isvoid
, where asPartial
returnsMvcHtmlString
Syntax for invoking
Partial()
andRenderPartial()
methods in Razor viewsSyntax for invoking
Partial()
andRenderPartial()
methods in webform viewsThe following are the 2 common interview questions related to
Partial()
andRenderPartial()
When would you use
Partial()
overRenderPartial()
and vice versa?The main difference is that
RenderPartial()
returns void and the output will be written directly to the output stream, where as thePartial()
method returnsMvcHtmlString
, which can be assigned to a variable and manipulate it if required. So, when there is a need to assign the output to a variable for manipulating it, then use Partial(), else use RenderPartial().Which one is better for performance?
From a performance perspective, rendering directly to the output stream is better.
RenderPartial()
does exactly the same thing and is better for performance overPartial()
.Partial 或 RenderPartial: 无需创建操作方法。当要在部分视图上显示的数据已存在于当前页面的模型中时使用。
Action 或 RenderAction: 需要子操作方法。当视图上显示的数据具有独立模型时使用。
Partial or RenderPartial: No need to create action method. use when data to be display on the partial view is already present in model of current page.
Action or RenderAction: Requires child action method. use when data to display on the view has independent model.
Html.RenderAction
的返回类型为void
,这意味着它直接在 View 中渲染响应,而Html.Action
的返回类型为>MvcHtmlString
您可以在控制器中捕获其渲染视图并使用以下方法对其进行修改这将返回视图的 Html 字符串。
这也适用于
Html.Partial
和Html.RenderPartial
The return type of
Html.RenderAction
isvoid
that means it directly renders the responses in View where the return type ofHtml.Action
isMvcHtmlString
You can catch its render view in controller and modify it by using following methodThis will return the Html string of the View.
This is also applicable to
Html.Partial
andHtml.RenderPartial
Html.Partial
:返回MvcHtmlString
和缓慢的Html.RenderPartial
:直接在输出流上渲染/写入并返回void
与Html.Partial
相比,它的速度非常快Html.Partial
: returnsMvcHtmlString
and slowHtml.RenderPartial
: directly render/write on output stream and returnsvoid
and it's very fast in comparison toHtml.Partial
@Html.Partial
返回 HTML 编码字符串中的视图并使用相同的视图TextWriter
对象。@Html.RenderPartial
此方法返回void
。@Html.RenderPartial
比@Html.Partial
更快PartialView
的语法:@Html.Partial
returns view in HTML-encoded string and use same viewTextWriter
object.@Html.RenderPartial
this method returnvoid
.@Html.RenderPartial
is faster than@Html.Partial
The syntax for
PartialView
:对于“部分”,我总是按如下方式使用它:
如果您需要在页面中包含一些需要通过控制器进行的内容(就像使用 Ajax 调用一样),则使用“Html.RenderPartial”。
例如,如果您有一个未链接到控制器本身且仅位于“共享”文件夹中的“静态”包含,请使用“HTML.partial”
For "partial" I always use it as follows:
If there's something you need to include in a page that you need to go via the controller (like you would with an Ajax call) then use "Html.RenderPartial".
If you have a 'static' include that isn't linked to a controller per-se and just in the 'shared' folder for example, use "HTML.partial"