When to use RenderPartial, Partial, RenderAction or Action in MVC
In ASP.NET MVC you can render data from partial views by using RenderPartial, Partial,
RenderAction or Action.
At times this can be confusing. In this article we look at the different methods in a little more depth
to try and clarify why and when you would use which method.
Render or not to Render
Both Partial and Action have each a possible alternative, RenderPartial
and RenderAction. The main difference is that Partial and Action both
return the generated HTML as a MvcHtmlString, while both RenderPartial and
RenderAction return void writing the generated HTML directly to the response.
Use RenderAction or RenderPartial if you just want to display the generated HTML.
Writing directly to the response is extremely efficient specially when dealing with a rather large amount of HTML.
Use Action or Partial if you need your response to come back as a string.
For example you might return JSON for further processing or need the generated HTML in a string for
further manipulation or formatting.
Action vs Partial
For the purpose of this section I will only refer to Action and Partial
but off course it applies such the same to the Render counterparts.
Lets take a closer look at just the differences between Action and Partial.
What my very basic and crude diagram is trying to show, is how Partial methods are limited by the context of the
current controller action. Though you can pass model data to Partial methods it will come from the model currently used in your view.
In addition the ViewData is also passed by default to a Partial method, though you can specify custom ViewData as well.
Action on the other hand allows you to call out to another controller and creating a
new model containing completely unrelated data, passed to the partial view before returning it back to the view.
This hints at when we should use a Partial method and when we should use an Action method.
When to use a Partial method
Use a Partial method if you have related data you want to render through a partial view for which you already have the
data as part of the model. For example if you are creating a page to render a blog post you would most likely have a model
containing the post content but you also contain the comments. You are using a partial view for your comments
and would use one of the Partial methods, passing the comments from the current model as the model to the call.
When to use an Action method
Use an Action method if you have data you want to render through a partial view which is not directly related
but may be useful to show on the page as well. For example if we stick with the page that renders a blog post, that
page could also show a list of previous articles.
Previous articles are not really related to the current post so by design
it would not make sense to include them in the same model you currently have. In fact, you might even want to display
a list of previous posts on every page or only on a few specific pages. You are using a partial view for your list of
previous posts and would use one of the Action methods, specifying the controller and action which will
create the model for the list of previous articles and passes it on to the partial view which we are displaying as part of our page.
Additional Information
Unlike the Partial methods, when calling Action methods a new instances of the
controller is created for every call.
Action methods are the best choice to cache the result of a controller action so that the same content
does not need to be created each time the action is executed. That is because Partial methods ignore
[OutputCache()] directives.
