Partial View in ASP.NET MVC

Level : Beginner
Mentor: Shailendra Chauhan
Duration : 00:02:00

What is Partial View in ASP.NET MVC?

In ASP.NET Web forms, partial views are similar to user controls and are utilized to facilitate code reuse. The use of partial views helps in minimizing code duplication. As a result, reusable views like Header and Footer views are partial views. A partial view can be used to show the calendar, social bookmark buttons, blog comments, product categories, dynamic ticker, and more.

Types of Partial Views in ASP.NET MVC

Static Partial View

Rendering static data or pre-defined information is the purpose of the static partial view. When there is no requirement for the partial view content to communicate with the controller, it is utilized.

  • @{Html.RenderPartial("_PartialView");}: This method renders the partial view straight into the response stream, giving no return value.
  • @Html.Partial("_PartialView");: The HTML string that represents the partial view content is returned by @Html.Partial("_PartialView"); it can be immediately included in the view or assigned to a variable.

Dynamic Partial View

This view uses a controller action to render dynamic data, which enables the partial view to get extra processing or a model.

  • @{Html.RenderAction("_PartialView");}: This function renders the partial view that results immediately in the response stream after executing the provided action.
  • @Html.Action("_PartialView");: This function runs the given action method and outputs an HTML string representing the partial view that may be immediately included in the view or assigned to a variable.

Create the Partial View

To facilitate code reuse, a partial view in ASP.NET MVC is a reusable view component generated using a .cshtml file that can encapsulate common markup and be included in many views.

Render the Partial View in a View

A partial view can be rendered inside a parent view using the @Html.Partial or @Html.RenderPartial helper methods, smoothly incorporating the content of the partial view into the parent view.

Pass Data to Partial View

A partial view can receive data via a model or a ViewData dictionary, which enables the partial view to render dynamic content depending on the data it receives.

Use jQuery To Render A Partial View

Using jQuery and an AJAX request, a partial view can be dynamically loaded. This involves retrieving the HTML content of the partial view from the server and inserting it into a specific browser element.

Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 50+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this