Html.Partial vs Html.RenderPartial & Html.Action 与 Html.RenderAction

发布于 2024-10-21 15:22:00 字数 180 浏览 2 评论 0原文

之间有什么区别

  • 在 ASP.NET MVC 中, Html.PartialHtml.RenderPartial
  • Html.ActionHtml.RenderAction

In ASP.NET MVC, what is the difference between:

  • Html.Partial and Html.RenderPartial
  • Html.Action and Html.RenderAction

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

—━☆沉默づ 2024-10-28 15:22:00

Html.Partial 返回一个字符串。 Html.RenderPartial 在内部调用 Write 并返回 void

基本用法是:

// Razor syntax
@Html.Partial("ViewName")
@{ Html.RenderPartial("ViewName");  }

// WebView syntax
<%: Html.Partial("ViewName") %>
<% Html.RenderPartial("ViewName"); %>

在上面的代码片段中,两个调用将产生相同的结果。

虽然可以将 Html.Partial 的输出存储在变量中或从方法返回它,但不能使用 Html.RenderPartial 执行此操作。

结果将在执行/评估期间写入Response流。

这也适用于 Html.ActionHtml.RenderAction

Html.Partial returns a String. Html.RenderPartial calls Write internally and returns 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.Partial in a variable or return it from a method, one cannot do this with Html.RenderPartial.

The result will be written to the Response stream during execution/evaluation.

This also applies to Html.Action and Html.RenderAction.

雾里花 2024-10-28 15:22:00

将 @Html.Partial 视为复制到父页面中的 HTML 代码。
将 @Html.RenderPartial 视为合并到父页面中的 .ascx 用户控件。 .ascx 用户控件的开销要大得多。

'@Html.Partial' 返回一个与父级内联构造的 html 编码字符串。它访问父级的模型。

'@Html.RenderPartial' 返回 .ascx 用户控件的等效项。它获取自己的页面 ViewDataDictionary 副本,并且对 RenderPartial 的 ViewData 所做的更改不会影响父级的 ViewData。

使用反射我们发现:

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);
}

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:

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);
}
翻了热茶 2024-10-28 15:22:00

这是我发现的:

当您没有要发送到视图的模型并且有大量不需要存储在变量中的 html 要带回时,请使用 RenderAction

当您没有要发送到视图的模型并且需要带回一些需要存储在变量中的文本时,请使用操作

当您有一个模型要发送到视图并且有大量不需要存储在变量中的 html 时,请使用 RenderPartial

当您有一个模型要发送到视图并且需要将一些文本存储在变量中时,请使用Partial

RenderActionRenderPartial 速度更快。

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.

記柔刀 2024-10-28 15:22:00

区别在于第一个返回 MvcHtmlString,但第二个 (Render..) 直接输出到响应。

Difference is first one returns an MvcHtmlString but second (Render..) outputs straight to the response.

匿名的好友 2024-10-28 15:22:00

@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.

东京女 2024-10-28 15:22:00

根据我的说法,@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()

春庭雪 2024-10-28 15:22:00

关于这个问题的更多信息:

“当仅使用部分名称调用 Html.RenderPartial() 时
视图,ASP.NET MVC 会将相同的模型传递给分部视图
调用视图模板使用的 ViewData 字典对象。”

来自 Professional ASP.NET MVC 1.0 的“NerdDinner”

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."

“NerdDinner” from Professional ASP.NET MVC 1.0

数理化全能战士 2024-10-28 15:22:00

区别:

  1. RenderPartial的返回类型是void,而Partial返回MvcHtmlString< /code>

  2. 在 Razor 视图中调用 Partial()RenderPartial() 方法的语法

    <块引用>

    @Html.Partial("PartialViewName")
    @{ Html.RenderPartial("PartialViewName"); }

  3. 在 Web 表单视图中调用 Partial()RenderPartial() 方法的语法

[%: Html.Partial("PartialViewName") %]
[% Html.RenderPartial("PartialViewName"); %]

以下是与 Partial()RenderPartial() 相关的 2 个常见面试问题
什么时候会使用 Partial() 而不是 RenderPartial() ,反之亦然?

主要区别在于 RenderPartial() 返回 void,输出将直接写入输出流,而 Partial() 方法返回 MvcHtmlString,可以将其分配给变量并根据需要对其进行操作。因此,当需要将输出分配给变量以对其进行操作时,请使用 Partial(),否则使用 RenderPartial()。

哪一个性能更好?

从性能角度来看,直接渲染到输出流更好。 RenderPartial() 执行完全相同的操作,并且性能优于 Partial()

Differences:

  1. The return type of RenderPartial is void, where as Partial returns MvcHtmlString

  2. Syntax for invoking Partial() and RenderPartial() methods in Razor views

    @Html.Partial("PartialViewName")
    @{ Html.RenderPartial("PartialViewName"); }

  3. Syntax for invoking Partial() and RenderPartial() methods in webform views

[%: 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?

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().

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().

峩卟喜欢 2024-10-28 15:22:00

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.

如果没有 2024-10-28 15:22:00

Html.RenderAction 的返回类型为 void,这意味着它直接在 View 中渲染响应,而 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();
        }
    }

这将返回视图的 Html 字符串。

这也适用于 Html.PartialHtml.RenderPartial

The return type of Html.RenderAction is void that means it directly renders the responses in View where the return type of Html.Action is MvcHtmlString You can catch its render view in controller and modify it by using following method

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.

This is also applicable to Html.Partial and Html.RenderPartial

奶茶白久 2024-10-28 15:22:00

Html.Partial:返回 MvcHtmlString 和缓慢的

Html.RenderPartial:直接在输出流上渲染/写入并返回 voidHtml.Partial 相比,它的速度非常快

Html.Partial: returns MvcHtmlString and slow

Html.RenderPartial: directly render/write on output stream and returns void and it's very fast in comparison to Html.Partial

予囚 2024-10-28 15:22:00

@Html.Partial 返回 HTML 编码字符串中的视图并使用相同的视图 TextWriter 对象。
@Html.RenderPartial 此方法返回void
@Html.RenderPartial@Html.Partial 更快

PartialView 的语法:

 [HttpGet] 
 public ActionResult AnyActionMethod
 {
     return PartialView();
 }

@Html.Partial returns view in HTML-encoded string and use same view TextWriter object.
@Html.RenderPartial this method return void.
@Html.RenderPartial is faster than @Html.Partial

The syntax for PartialView:

 [HttpGet] 
 public ActionResult AnyActionMethod
 {
     return PartialView();
 }
つ低調成傷 2024-10-28 15:22:00

对于“部分”,我总是按如下方式使用它:

如果您需要在页面中包含一些需要通过控制器进行的内容(就像使用 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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文