Partial View Result
A partial view is a reusable portion of a web page.
ASP.NET provides a facility to create such reusable portion of code, that can be shared inside the same web application. These sharable views are known as partial views.
It can be used in one or more Parent Views or Layout Views. You can use the same partial view at multiple places and eliminates the redundant code.
This view file has the same extension as other views have .cshtml.
Partial views are reusable views like as Header and Footer views.
A partial view is like as user control in Asp.Net Web forms that is used for code re-usability.
Create a New Partial View
To create a partial view, right click on the Views folder > Shared folder -> click Add -> click View (in Higher version select MVC 5 View) -> Add
You can create a partial view in any View folder. However, it is recommended to create all your partial views in the Shared folder so that they can be used in multiple views.
In the Add New Item popup, enter a partial view name, select "Create as a partial view" checkbox.
Select Model Class in Template dropdown, if want to use a model inside our Partial view other wise keep the Template dropdown as Empty (without model) and
click on Add button. This will create an empty partial view in the Shared folder.
Add Necessary Code for Header Inside this Partial View
Rendering a Partial View
You can render the partial view in the parent view using the HTML helper methods: @html.Partial(), @html.RenderPartial(), and @html.RenderAction().
Html.Partial()
The @Html.Partial() method renders the specified partial view.
It accepts partial view name as a string parameter and returns MvcHtmlString.
It renders the specified partial view as an HTML-encoded string, so you have a chance of modifying the HTML before rendering.
Visit docs.microsoft.com to know the overloads of the Partial() method.
Overlaoded Signatures of Partial Helper Methods
Partial(String, Object)
Partial(String, Object, ViewDataDictionary)
Partial(String, ViewDataDictionary)
Html.RenderPartial()
The @html.RenderPartial() method is the same as the @html.Partial() method except that it writes the resulted HTML of a specified partial view into an HTTP response stream directly.
So, you can modify it's HTML before render.
The RenderPartial() method returns void, so a semicolon is required at the end, and so it must be enclosed within the @{ }.
RenderPartial() has better performance than Partial().
Overlaoded Signatures of RenderPartial Helper Methods
RenderPartial(String, Object)
RenderPartial(String, Object, ViewDataDictionary)
RenderPartial(String, ViewDataDictionary)
Html.RenderAction()
The @html.RenderAction() method executes the specified action method and renders the result.
To render a partial view using the RenderAction() method, first create an HttpGet action method and apply the [ChildActionOnly] attribute and return the PartialViewResult using the PartialView() method.