+91-90427 10472
         
Dot net training in Chennai

Partial View in MVC – ASP.NET

Partial View in MVC:

In ASP.NET MVC, a partial view is a special view that renders only a portion of the view content. It’s similar to a web form application’s user control. Partial can be reused in different views. It aids us in reducing code duplication. To put it another way, a partial view allows us to render a view within the parent view.

The partial view is implemented with its own copy of the parent view’s ViewDataDictionary object, so that the partial view can access the parent view’s data. The parent view’s data is unaffected if we change this data (ViewDataDictionary object). When related data that we want to render in a partial view is part of our model, we generally use the Partial rendering method of the view.

A partial view is a Razor markup file (.cshtml) without a @page directive that renders HTML output within the rendered output of another markup file.

When developing an MVC app, where markup files are called views, or a Razor Pages app, where markup files are called pages, the term partial view is used. This topic refers to MVC views and Razor Pages pages as markup files in general.

Difference between view and partial view in MVC: View is a complete markup that may include a master view (or master page) with all the design(s), whereas partial view is only a portion of the page or a small markup that does not include a master page. It is primarily used as a user control in mvc and can be applied to multiple views.

When to use partial views

Partial views are a useful technique when:

  • Large markup files should be divided into smaller parts. i.e. There is a benefit to working with each logical item separately in a partial view when working with a large, sophisticated markup file that is divided into numerous logical sections. Because the markup only provides references to partial views and the overall page structure, it is possible to handle the code in the markup file.
  • Reduce the amount of common markup content that is duplicated across markup files. i.e. When the same markup elements are used in multiple markup files, a partial view consolidates the markup content into a single partial view file. When the markup in the partial view is changed, the rendered output of the markup files that use the partial view is updated.

Note:

Partial views should not be used to keep common layout elements in place. _Layout.cshtml files should contain common layout elements.

When complex rendering logic or code execution is required to render the markup, do not use a partial view. Use a view component instead of a partial view in such cases.

How to Declare partial views

The names of partial views frequently begin with an underscore (_). This naming convention is optional, but it aids in visually distinguishing partial views from views and pages.

A partial view is a.cshtml markup file that is kept in the Views folder (MVC) or Pages folder(Razor Pages) but does not contain a @page directive

A controller’s ViewResult in ASP.NET Core MVC can return a full view or a partial view. A PageModel can return a partial view in Razor Pages that is represented as a PartialViewResult object. The Reference a partial view section goes over rendering and referencing partial views.