Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 53 - Difference between html.partial and html.renderpartial

Suggested Videos 
Part 50 - Detect errors in views at compile time
Part 51 - Advantages of using strongly typed views
Part 52 - Partial views in mvc



In this video, we will 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 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().

2 comments:

  1. Hi Venkat Sir,

    @ is missing here.

    { Html.RenderPartial("PartialViewName"); }

    ReplyDelete
  2. It depends on where you are placing that RenderPartial method. If you are placing under Div tag or any HTML tag then it's required, otherwise not required.

    Suppose, If it is under div then it should be

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

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.