.net Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5248183/
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
Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction
提问by Ghooti Farangi
In ASP.NET MVC, what is the difference between:
在 ASP.NET MVC 中,有什么区别:
Html.PartialandHtml.RenderPartialHtml.ActionandHtml.RenderAction
Html.Partial和Html.RenderPartialHtml.Action和Html.RenderAction
回答by GvS
Html.Partialreturns a String. Html.RenderPartialcalls Writeinternally and returns void.
Html.Partial返回一个字符串。内部Html.RenderPartial调用Write并返回void.
The basic usage is:
基本用法是:
// Razor syntax
@Html.Partial("ViewName")
@{ Html.RenderPartial("ViewName"); }
// WebView syntax
<%: Html.Partial("ViewName") %>
<% Html.RenderPartial("ViewName"); %>
In the snippet above, both calls will yield the same result.
在上面的代码段中,两个调用都会产生相同的结果。
While one can store the output of Html.Partialin a variable or return it from a method, one cannotdo this with Html.RenderPartial.
虽然可以将 的输出存储Html.Partial在变量中或从方法中返回它,但不能使用Html.RenderPartial.
The result will be written to the Responsestream during execution/evaluation.
结果将Response在执行/评估期间写入流。
This also applies to Html.Actionand Html.RenderAction.
这也适用于Html.Action和Html.RenderAction。
回答by Brett Jones
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 视为复制到父页面中的 HTML 代码。将@Html.RenderPartial 视为合并到父页面中的 .ascx 用户控件。.ascx 用户控件的开销要大得多。
'@Html.Partial'returns a html encoded string that gets constructed inline with the parent. It accesses the parent's model.
'@Html.Partial'返回一个 html 编码的字符串,该字符串与父级内联构造。它访问父模型。
'@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.
'@Html.RenderPartial'返回等效的 .ascx 用户控件。它获取自己的页面 ViewDataDictionary 副本,对 RenderPartial 的 ViewData 所做的更改不会影响父级的 ViewData。
Using reflection we find:
使用反射我们发现:
public static MvcHtmlString Partial(this HtmlHelper htmlHelper, string partialViewName, object model, ViewDataDictionary viewData)
{
MvcHtmlString mvcHtmlString;
using (StringWriter stringWriter = new StringWriter(CultureInfo.CurrentCulture))
{
htmlHelper.RenderPartialInternal(partialViewName, viewData, model, stringWriter, ViewEngines.Engines);
mvcHtmlString = MvcHtmlString.Create(stringWriter.ToString());
}
return mvcHtmlString;
}
public static void RenderPartial(this HtmlHelper htmlHelper, string partialViewName)
{
htmlHelper.RenderPartialInternal(partialViewName, htmlHelper.ViewData, null, htmlHelper.ViewContext.Writer, ViewEngines.Engines);
}
回答by David
Here is what I have found:
这是我发现的:
Use RenderActionwhen 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.
当您没有要发送到视图的模型并且有大量不需要存储在变量中的 html 需要返回时,请使用RenderAction。
Use Actionwhen 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.
当您没有要发送到视图的模型并且有一些需要存储在变量中的文本要带回来时,请使用Action。
Use RenderPartialwhen 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.
当您有一个模型要发送到视图时使用RenderPartial,并且会有很多不需要存储在变量中的 html。
Use Partialwhen 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.
当您有一个模型要发送到视图并且需要将一些文本存储在变量中时,请使用Partial。
RenderActionand RenderPartialare faster.
RenderAction和RenderPartial更快。
回答by Aliostad
Difference is first one returns an MvcHtmlStringbut second (Render..) outputs straight to the response.
不同之处在于第一个返回一个MvcHtmlString但第二个 ( Render..) 输出直接到响应。
回答by iChirag
According to me @Html.RenderPartial()has faster execution than @Html.Partial()due to Html.RenderPartial gives a quick response to Output.
据我说@Html.RenderPartial(),执行速度比@Html.Partial()Html.RenderPartial更快,因此可以快速响应输出。
Because when I use @Html.Partial(), my website takes more time to load compared to @Html.RenderPartial()
因为当我使用 时@Html.Partial(),我的网站加载时间比 @Html.RenderPartial()
回答by Jayesh Patel
@Html.Partialand @Html.RenderPartialare used when your Partial view model is correspondence of parent model, we don't need to create any action method to call this.
@Html.Partial并且@Html.RenderPartial在您的局部视图模型是父模型的对应关系时使用,我们不需要创建任何操作方法来调用它。
@Html.Actionand @Html.RenderActionare 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.Action并且@Html.RenderAction在您的局部视图模型独立于父模型时使用,基本上当您想在页面上显示任何小部件类型内容时使用它。您必须创建一个操作方法,该方法在从视图调用方法时返回部分视图结果。
回答by Ygor Thomaz
More about the question:
关于这个问题的更多信息:
"When Html.RenderPartial() is called with just the name of the partial view, ASP.NET MVC will pass to the partial view the same Model and ViewData dictionary objects used by the calling view template."
“当仅使用局部视图的名称调用 Html.RenderPartial() 时,ASP.NET MVC 会将调用视图模板使用的相同 Model 和 ViewData 字典对象传递给局部视图。”
“NerdDinner” from Professional ASP.NET MVC 1.0
来自 Professional ASP.NET MVC 1.0 的“NerdDinner”
回答by Shivkumar
The return type of Html.RenderActionis voidthat means it directly renders the responses in View where the return type of Html.Actionis MvcHtmlStringYou can catch its render view in controller and modify it by using following method
的返回类型Html.RenderAction是void指它直接在视图中渲染响应,其中返回类型Html.Action是 MvcHtmlString您可以在控制器中捕获其渲染视图并使用以下方法进行修改
protected string RenderPartialViewToString(string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = ControllerContext.RouteData.GetRequiredString("action");
ViewData.Model = model;
using (StringWriter sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
This will return the Html string of the View.
这将返回视图的 Html 字符串。
This is also applicable to Html.Partialand Html.RenderPartial
这也适用于Html.Partial和Html.RenderPartial
回答by Anil
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.
Partial 或 RenderPartial:无需创建动作方法。当要在局部视图上显示的数据已经存在于当前页面的模型中时使用。
Action or RenderAction:Requires child action method. use when data to display on the view has independent model.
Action 或 RenderAction:需要子操作方法。当要在视图上显示的数据具有独立模型时使用。
回答by Nazmul Hasan
Differences:
区别:
The return type of
RenderPartialisvoid, where asPartialreturnsMvcHtmlStringSyntax for invoking
Partial()andRenderPartial()methods in Razor views@Html.Partial("PartialViewName")
@{ Html.RenderPartial("PartialViewName"); }Syntax for invoking
Partial()andRenderPartial()methods in webform views
的返回类型
RenderPartialisvoid,其中 asPartial返回MvcHtmlStringRazor 视图中调用
Partial()和RenderPartial()方法的语法@Html.Partial("PartialViewName")
@{ Html.RenderPartial("PartialViewName"); }Webform 视图中调用
Partial()和RenderPartial()方法的语法
[%: Html.Partial("PartialViewName") %]
[% Html.RenderPartial("PartialViewName"); %]
[%: Html.Partial("PartialViewName") %]
[% Html.RenderPartial("PartialViewName"); %]
The following are the 2 common interview questions related to Partial()and RenderPartial()When would you use Partial()over RenderPartial()and vice versa?
以下是与Partial()和RenderPartial()何时使用Partial()overRenderPartial()和反之亦然相关的 2 个常见面试问题?
The main difference is that RenderPartial()returns void and the output will be written directly to the output stream, where as the Partial()method returns MvcHtmlString, 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().
主要区别在于RenderPartial()返回 void 并且输出将直接写入输出流,其中Partial()方法返回MvcHtmlString,可以将其分配给变量并在需要时对其进行操作。因此,当需要将输出分配给变量以进行操作时,请使用 Partial(),否则使用 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 over Partial().
从性能的角度来看,直接渲染到输出流更好。RenderPartial()做完全相同的事情,并且性能优于Partial().

