Html.Partial vs Html.RenderPartial & Html.Action v

2018-12-31 05:46发布

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

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

13条回答
低头抚发
2楼-- · 2018-12-31 06:06

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

查看更多
看淡一切
3楼-- · 2018-12-31 06:06

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

查看更多
心情的温度
4楼-- · 2018-12-31 06:08

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.

查看更多
永恒的永恒
5楼-- · 2018-12-31 06:09

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.

查看更多
刘海飞了
6楼-- · 2018-12-31 06:15

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

查看更多
浮光初槿花落
7楼-- · 2018-12-31 06:17

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"

查看更多
登录 后发表回答