PartialViewResult is a specific type of ActionResult designed for returning partial views from your controller actions. Partial views, as you know, are reusable chunks of Razor markup (.cshtml) that can be embedded within other views. However, instead of being rendered as a full page, they’re intended to be a fragment of HTML that can be inserted into a larger view.
Why Use PartialViewResult? Dynamic Content Delivery: You can use PartialViewResult to load content dynamically into your main view using AJAX or other client-side techniques. Separation of Concerns: This result type helps keep your controller actions focused on returning data and leaves the rendering of that data to the view. Simplified Testing: Since PartialViewResult doesn’t involve rendering a complete page, it’s often easier to unit test your action methods that return partial views.
Creating a PartialViewResult In your controller actions, you can return a PartialViewResult in a couple of ways: Direct Instantiation: return new PartialViewResult { ViewName = “_ListPartialView”, // Name of the partial view ViewData = new ViewDataDictionary(ViewData, model) // Pass the model data };
Using the PartialView() Helper Method: return PartialView(“_ListPartialView”, model); // Much simpler way The PartialView() method is a shorthand provided by the Controller base class for conveniently creating a PartialViewResult.
<script> document.querySelector(“#button-load”).addEventListener(“click”, async function() { var response = await fetch(“programming-languages”); var languages = await response.text(); document.querySelector(“.programming-languages-content”).innerHTML = languages; }); </script> This view contains a button, a div, and some JavaScript to load the partial view’s content when the user clicks a button using fetch function.
HomeController.cs (Controller) [Route(“programming-languages”)] public IActionResult ProgrammingLanguages() { ListModel listModel = new ListModel() { ListTitle = “Programming Languages List”, ListItems = new List<string>() { “Python”, “C#”, “Go” } };
return PartialView(“_ListPartialView”, listModel); } In this action method: Data Preparation: A ListModel object is created and populated with data for the list (title and items). Partial View Returned: The PartialView() method is used to return a PartialViewResult. The method takes two arguments: “_ListPartialView”: The name of the partial view file (located in Views/Shared by default). listModel: The model data to pass to the partial view. When this action method is called via the URL /programming-languages by the javascript code on the page, it returns the partial view along with the data that is then injected into the div with class programming-languages-content by the javascript.
Important Points: AJAX and Dynamic Loading: PartialViewResult is frequently used in conjunction with AJAX to load content dynamically without full page refreshes. View Model (Best Practice): Always try to use a view model to pass data to your partial views for better type safety and maintainability. Caching: Consider using output caching on your partial views to improve performance if they render data that doesn’t change frequently.
ViewData in Partial Views Purpose: Pass data to partial views from the parent view or controller. Usage: In the parent view or controller: ViewData[“key”] = value In the partial view: @ViewData[“key”] ViewData is not strongly typed; be careful with type casting and null checks.
Strongly Typed Partial Views Purpose: Associate a partial view with a specific model class using the @model directive. Benefits: Type safety: Catches errors during development. IntelliSense: Code completion for model properties. Maintainability: Easier updates when models change. How to Use: Create a model class. Use @model YourModel in the partial view. Pass a model instance when rendering: <partial model=”yourModelInstance” /> or @await Html.PartialAsync(“_PartialName”, yourModelInstance)