Partial view enables you to render a view inside the parent view. Partial View is similar to User Control in ASP.NET Web Forms.
The partial view is instantiated with its own copy of a ViewDataDictionary object which is available with the parent view so that partial view can access the data of the parent view. Any changes to this data does not cause a change in Parent view.
Advantages of Partial View in ASP.net MVC:
using Partial View in ASP.NET MVC has following advantages:
Enhances reusability by packaging up common website code instead of repeating the same in different pages.
Easy to maintain. Changes in future are simple to accommodate.
Example:
Let’s take an example to understand Partial view better.
Visual Studio–> ASP.NET MVC application .
Now in Solution Explorer, go toViews -> Home folder. Then right click on Home folder and Choose Add -> View.
Use “ReusablePartialView” as View name and mark the check box as “Create as a Partial View” as shown in following screenshot.
Click on “Add” button. A new Partial View is created in same folder as that of Index View. Now, we can put some reusable content inside our Partial view.
Now, we need to use this partial view in main parent view (i.e. index view). Rendering a Partial View inside another View is quite simple by using Html.RenderPartial function and passing Partial view name as parameter to it.
<% Html.RenderPartial(“ReusablePartialView”); %>
Note: We need to ensure that Partial View exists in same folder as that of main view, otherwise, add the path for the partial view folder in RenderPartial function. Now if we run our ASP.NET MVC application, content will be rendered at the bottom as shown below:
SudhakarPosted Nov 29, 2017, 12:05 AM
In this article, we will learn:
What is Partial view in ASP.net MVC?
Partial view enables you to render a view inside the parent view. Partial View is similar to User Control in ASP.NET Web Forms.
The partial view is instantiated with its own copy of a ViewDataDictionary object which is available with the parent view so that partial view can access the data of the parent view. Any changes to this data does not cause a change in Parent view.
Advantages of Partial View in ASP.net MVC:
using Partial View in ASP.NET MVC has following advantages:
Example:
Let’s take an example to understand Partial view better.
<% Html.RenderPartial(“ReusablePartialView”); %>
Note: We need to ensure that Partial View exists in same folder as that of main view, otherwise, add the path for the partial view folder in RenderPartial function.
Now if we run our ASP.NET MVC application, content will be rendered at the bottom as shown below: