A partial view is like as user control in ASP.NET Web forms that is used for code re-usability. Partial views helps us to reduce code duplication. Below are four methods for rendering a partial view in ASP.NET MVC.

  • Partial( )
  • RenderPartial( )
  • Action( )
  • RenderAction( )

Let’s discuss more about these methods.

Html.Partial( ):

It is simple to use and no need to create any action method inside MVC controller. It will renders the partial view as an HTML-encoded string. This method result can be stored in a variable, since it returns string value (i.e., HTML-encoded string). Partial method is useful when the displaying data in the partial view is already in the corresponding view model.

Syntax:

@Html.Partial("_TopMenu")

 

Html.RenderPartial( ):

It is simple to use and no need to create any action method inside MVC controller. This method result will be directly written to the HTTP response stream. This method returns void and is faster than Partial method since its result is directly written to the response stream which makes it fast. Like Partial method, RenderPartial method is also useful when the displaying data in the partial view is already in the corresponding view model.

Syntax:

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

 

Html.Action( ):

For this method, we need to create a child action for the rendering the partial view. It will renders the partial view as an HtmlString. This method result can be stored in a variable, since it returns string type value. This method is also the best choice when you want to cache a partial view. Action method is useful when the displaying data in the partial view is independent from corresponding view model

Syntax:

@{Html.Action("Orders","Home");}

 

Html.RenderAction( ):

For this method, we need to create a child action for the rendering the partial view. This method result will be directly written to the HTTP response stream means it used the same TextWriter object as used in the current webpage/template. Like Action method, RenderAction() method is also useful when the displaying data in the partial view is independent from corresponding view model.

 Syntax:

@{Html.RenderAction("Orders","Home");}

 

 Hope this article provided you some basic knowledge on partial view.

Happy Coding 🙂