.netASP.Net MVC

Partial View in ASP.net MVC

In this article, we will learn:

  • What is Partial view in ASP.net MVC
  • Advantages of Partial view
  • Example

What is Partial view in ASP.net MVC?

  • Partial View in ASP.NET MVC serves the same purpose as that of a User Control in ASP.NET Web Forms.
  • In order to reuse a View in our MVC application (For example, placing a header, list of items or footer etc.),
  • Partial Views are introduced by Microsoft that basically renders inside a 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.
  • ASP.net Partial view
  • 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:

 

ASP.net Partial View

© 2015, www.techkatak.com. All rights reserved.

One thought on “Partial View in ASP.net MVC

Comments are closed.