What is partial view?
Partial view is a like as user control in asp.net that is used for re-usability.
Using partial view you can reduce the code duplication. Therefore partial views
are reusable view like as header and footer views.
When to use?
You can use partial view to display common things like blog comments, product
category, social bookmarks buttons, a dynamic ticker, calendar etc.
How to create a partial view?
To create a partial view do the right click on the shared folder under views
in the solution explorer after that click on Add New View option then give the
name for the partial view.
The best practice to create partial view partial view name is preceded by "_",
but it is not mandatory. The "_" before view name specify that it is a reusable
component i.e. partial view.
Partial View Rendering
Using the ViewUserControl class that is derived from the user control class
partial view is rendered. There are three helper methods which are: Partial,
RenderPartial and Render action. These are used to render the partial view.
- <div> @Html.Partial("_Partial") </div>
- <div> @{Html.RenderPartial("_Partial");} </div>
The difference between above two methods is that the Partial helper method
renders a partial view into a string while RenderPartial method writes directly
into the response stream instead of returning a string.
- <div> @{Html.RenderAction("_Partial","Account");} </div>
Partial View Render using JQuery
If you want to load a partial view with in a popup model on run time then
you can use jQuery to make a ajax request and render a partial view into the
popup model. To load a partial view with in a div you need to do something like
as:
- <script>
- $('#div').load('/shared/_Partial’);
- </script>