30
MarPartial View in ASP.NET MVC
Partial View in ASP.NET MVC: An Overview
In this MVC Tutorial, We are going to explain the Partial View of ASP.NET MVC. A partial view is like a user control in Asp.Net Web forms that are used for code re-usability. Partial views help us to reduce code duplication. Hence partial views are reusable views like Header and Footer views.
We can use a partial view to display blog comments, product categories, social bookmark buttons, a dynamic ticker, a calendar, etc. For understanding the different rendering ways of partial view refer to the article RenderPartial vs RenderAction vs Partial vs Action in MVC Razor
Creating A Partial View
A partial view has the same file extension(.cshtml) as a regular view. To create a partial view, click on shared folder (\Views\Shared) in Solution Explorer click on the "Add New View" option, and then give the name for the partial view and also check the Create a partial view option as shown in Fig.
Note
It is best practice to create a partial view in the shared folder and the partial view name is preceded by "_", but it is not mandatory. The "_" before the view name specifies that it is a reusable component i.e. partial view.
Rendering Partial View
A partial view is rendered by using the ViewUserControl class that is inherited/derived from the ASP.NET UserControl class. The Partial, RenderPartial, and RenderAction helper methods are used to render partial view in mvc3 razor.
<div> @Html.Partial("_Comments") </div>
<div> @{Html.RenderPartial("_Comments");} </div>
The main difference between the above two methods is that the Partial helper method renders a partial view into a string while the RenderPartial method writes directly into the response stream instead of returning a string.
<div> @{Html.RenderAction("_Category","Home");} </div>
Note
Partial or RenderPartial methods are used when a model for the page is already populated with all the information. For example in a blog to show an article comment we would like to use Partial or RenderPartial methods since article information is already populated in the model.
The redirection method is used when some information is needed to show on multiple pages. Hence partial view should have its own model. For example, to categorize the list of articles on every page we would like to use the RenderAction method since the list of categories is populated by different models.
Render Partial View Using jQuery
Sometimes we need to load a partial view within a popup on run time like as a login box, then we can use jQuery to make an AJAX request and render a Partial View into the popup. To load a partial view within a div we need to do like as:
<script type="text/jscript">
$('#divpopup').load('/shared/_ProductCategory’);
</script>
Summary
I hope you will enjoy these tips and tricks while programming with Asp.Net MVC. I would like to have feedback from my blog readers. Your valuable feedback, questions, or comments about this article are always welcome. Enjoy Coding..!