Exploiting Partial and RenderPartial in ASP.NET MVC
If you're wondering what the difference is between the HtmlHelper Partial and RenderPartial methods, then here's why Peter, at least, uses Partial.
You may have noticed that the HtmlHelper IntelliSense list has two methods for integrating Partial Views into your View: Partial and RenderPartial. If you've ever used the HtmlHelper Action method you may have noticed that it, also, has two versions: Action and RenderAction.
From the point of view of grabbing Partial View, both Partial and RenderPartial look very much alike. They both, for example, accept up to three parameters: name of the Partial View, an object (which will override the Model property of the View) and a collection of type ViewDataDictionary (which will override the ViewData/ViewBag of the View).
From the coding point of view, the major difference is that you have to call RenderPartial inside a code block, like this:
That's also true when using Action and RenderAction.
The primary difference between the two methods is that Partial generates the HTML from the View and returns it to the View to be incorporated into the page. RenderPartial, on the other hand, doesn't return anything and, instead, adds its HTML directly to the Response object's output. It's because RenderPartial doesn't return a result that you have to enclose it in a code block.
Therefore, in theory, RenderPartial should give you better performance … but I've yet to find someone who's done a comparison (your input is welcome, if you have). Again, the same should be true of Action and RenderAction.
But, ignoring performance, there are some other differences. For example, because the Render* methods aren't called "in line" like the Partial method, they get their own separate copies of the View's model object and ViewData/ViewBag collection. If the Partial View or Action makes changes to the model object or the ViewData/ViewBag, those changes won't be visible to the code in the View (though why you'd be making changes to those items in a Partial View is a mystery to me).
Having said all that, I typically use the Partial method instead of RenderPartial. I do that for two reasons: it's easier to type and it supports debugging. I use Partial Views a lot and, as a result, seeing what output my Partial View is adding to my page is very useful. Because the Partial method returns the HTML as a string I can, when debugging, copy the code from my View, paste it into the Immediate Window, press the key, and see what my Partial View is generating.
Of course, there's nothing stopping me from using RenderPartial in my View and Partial in the Immediate Window, except that I wouldn't be able to just copy the line of code from one window to the other. So, I guess, I should add a third reason: I'm lazier than anyone would have thought possible.
Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.