Part 53 - html.partial 和 html.renderpartial的区别

来源:互联网 发布:网络监控工作的短板 编辑:程序博客网 时间:2024/05/22 05:26

Let's discuss the difference between Partial() and RenderPartial() html helper methods. Both of these helper methods are used for rendering partial views. 

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

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

0 0
原创粉丝点击