Practical .NET

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:

@Code
  Html.RenderPartial("People")
End Code

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.

About the Author

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/.

comments powered by Disqus

Featured

  • Visual Studio 2019 for Mac v8.9 Ships with .NET 6 Preview 1 Support

    During its Ignite 2021 online event for IT pros and developers this week, Microsoft shipped Visual Studio 2019 for Mac v8.9, arriving with out-of-the-box support for .NET 6 Preview 1, which the company also released recently.

  • Analyst: TypeScript Now Firmly in Top 10 Echelon (Ruby, Not So Much)

    RedMonk analyst Stephen O'Grady believes TypeScript has achieved the rare feat of firmly ensconcing itself into the top 10 echelon of his ranking, now questioning how high it might go.

  • Black White Wave IMage

    Neural Regression Using PyTorch: Training

    The goal of a regression problem is to predict a single numeric value, for example, predicting the annual revenue of a new restaurant based on variables such as menu prices, number of tables, location and so on.

  • Microsoft Ships Visual Studio 2019 v16.9 Servicing Baseline Release

    Microsoft is urging enterprises and professional coders to standardize on the new Visual Studio 2019 v16.9, a servicing baseline release that's guaranteed to receive official support for an extended period.

Upcoming Events