Partial view is special view which renders a portion of view content. It is just like a user control web form application. If you want to reuse a view in your web application, you can go for the partial view concept. Partial view is like a regular view with a file extension .cshtml. We can use partial views in a situation where we need a header, footer reused for an MVC web application.

To create a partial view, right-click on view -> shared folder and select Add -> View option. In this way we can add a partial view.

It is not mandatory to create a partial view in a shared folder but a partial view is mostly used as a reusable component, it is a good practice to put it in the "shared" folder.

HTML helper has two methods for rendering the partial view: Partial and RenderPartial.

@Html.RenderPartial

The result of the RenderPartial method is written directly into the HTTP response, it means that this method used the same TextWriter object as used by the current view. This method returns nothing.

@Html.Partial

This method renders the view as an HTML-encoded string. We can store the method result in a string variable.

Difference between View and Partial View

 View Partial View
View contains the layout page Partial view does not contain the layout page
_viewstart page is rendered before any view is rendered Partial view does not check for a _viewstart.cshtml. We cannot place any common code for a partial view within the _viewStart.cshtml page.
View may have markup tags like html, body, head, title, meta etc. The Partial view is specially designed to render within the view and as a result it does not contain any mark up.

Partial view is more lightweight than the view. We can also pass a regular view to the RenderPartial method.

If there is no layout page specified in the view, it can be considered as a partial view. In razor, there is no distinction between views and partial views as in the ASPX view engine (aspx and ascx).

0 Comments1

Sorry, No more post.

Your Comment

Login To Comment

discard

By posting your Comment, you agree to the privacy policy and terms of service.