Virtual Classes Logo          Virtual Classes
    Read Anytime,Anywhere

Partial View

Partial view is special type of view which is like a ASP.NET user control.
Partial view is a reusable view which can be used in multiple views to render same kind of content, this way partial view helps to reduce code duplication in an application. Partial view is like a regular view with a file extension .cshtml.
you can use partial views in a situation where you have some content(e.g.header, footer etc) and you want these content to be displayed on every page. In this case, you can create a partial view which will have the common content and use partial view in other views in your web application.
partial view enables you to create a view that will be rendered inside a parent view and can access the data of the parent view.

Creating Partial View- To add a partial view in your application.
Right-click on view folder( or sub folder inside view folder) -> select Add -> select View option. Add Partial View in MVC

it will open a pop up as shown in below image.
Give a name to the view (I have given name _AddStudent to my partial view), select a view engine,check "Create as a Partial View " Option to create a partial view, and then click on add.
Add Partial View pop up

Partial view name is preceded by '_', because the '_' specifying that it is reusable view.

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

@Html.Partial("~/Views/Controls/_AddStudent.cshtml", student);
@Html.RenderPartial("~/Views/Controls/_AddStudent.cshtml", student);

@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.In Html.RenderPartial the result is written to the Response stream during execution. Html.RenderPartial calls Write internally, and returns void.
@Html.Partial:This method renders the view as an HTML-encoded string. We can store the method result in a string variable. Html.Partial returns a string,You can store the output of Html.Partial in a variable, or return it from a function.

The Html.RenderPartial method writes output directly to the HTTP response stream so it is slightly faster than the Html.Partial method.

Returning a Partial view from the Controller's Action method:
public ActionResult PartialViewExample()
return PartialView("Name of your partial view");