What is difference between Html.Partial
and Html.action
in context of using a partial view in asp.net MVC3 ?
In ASP.NET Core MVC, a controller's ViewResult is capable of returning either a view or a partial view. In Razor Pages, a PageModel can return a partial view represented as a PartialViewResult object. Referencing and rendering partial views is described in the Reference a partial view section.
To create a partial view, right-click on view -> shared folder and select Add -> View option. In this way we can add a partial view. It is not mandatory to create a partial view in a shared folder but a partial view is mostly used as a reusable component, it is a good practice to put it in the "shared" folder.
To call a partial view from another partial view, we follow the same approach as we follow when we call partial view from the content page. Let's take an example. This is the code of 1st partial view. Notice that in above partial view, we are calling another partial view named "_View2" that is created below.
In order to add Partial View, you will need to Right Click inside the Controller class and click on the Add View option in order to create a View for the Controller.
Html.Action will call a controller Action, so it'll go through the whole MVC pipeline (inside the server) again to find a controller that will return a ViewResult (although, you theoretically you can also return a JsonResult or something else) .
Html.Partial will only return a PartialPage (as in a CSHTML file) and won't go through the whole pipeline. It will just search using the view engine.
Some advantages to Action is having Authentication, Caching and other stuff that happens in the MVC pipeline, while Partial is faster (although you might have more responsibility in the partial page if you need to pass a ViewModel etc.)
This is a nice post (a bit old) about pros/cons of RenderAction vs RenderPartial
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With