Saturday, May 18, 2019

Partial views in Asp.Net Core MVC Views

A Partial View is another view page with .cshtml extension, Partial Views are used to modularize larger view pages and also to re-use common functionality across different views in the application.

Partial views improve maintainability by breaking down a large view page into modular views and embedding the partial views in the main view template. By doing this the markup code in the main view page reduces there by improving readability and maintainability.

Partial views also help reduce duplication of code in larger applications, if we have a common functionality / display content which is to be repeated in many places the a large application, then we can place the repeating content in a partial view and just embed the partial view wherever required. This reduces duplication of same logic in many places. Also improves maintainability since any change to the common logic needs to be done in only one place, in the partial view.

The syntax for including partial views varies by the version of Asp.Net Core used, in Asp.Net Core 2.1 we use the partial tag to include partial views as follows.


<partial name="_PartialViewName" />

Prior to Asp.Net Core 2.1, we need to use the HTML helper tag @Html.Partial to include a partial view as follows.

@Html.Partial("_PartialViewName")


We can also use the @Html.PartialAsync which will render the partial view asynchronously.

We can also use Html.RenderPartial (or) Html.RenderPartialAsync to render partial views.

The difference between Partial and RenderPartial methods is that the Partial method returns a HTML-encoded string while RenderPartial is a void method that writes directly to the response output stream.

Search Flipkart Products:
Flipkart.com

No comments: