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

  • Death of the Dev Machine?

    Here's a takeaway from this week's Ignite 2020 event: An advanced Azure cloud portends the death of the traditional, high-powered dev machine packed with computing, memory and storage components.

  • COVID-19 Is Ignite 2020's Elephant in the Room: 'Frankly, It Sucks'

    As in all things of our new reality, there was no escaping the drastic changes in routine caused by the COVID-19 pandemic during Microsoft's big Ignite 2020 developer/IT pro conference, this week shifted to an online-only event after drawing tens of thousands of in-person attendees in years past.

  • Visual Studio 2019 v16.8 Preview Update Adds Codespaces

    To coincide with the Microsoft Ignite 2020 IT pro/developer event, the Visual Studio dev team shipped a new update, Visual Studio 2019 v16.8 Preview 3.1, with the main attraction being support for cloud-hosted Codespaces, now in a limited beta.

  • Speed Lines Graphic

    New for Blazor: Azure Static Web Apps Support

    With Blazor taking the .NET web development world by storm, one of the first announcements during Microsoft's Ignite 2020 developer/IT event was its new support in Azure Static Web Apps.

  • Entity Framework Core 5 RC1 Is Feature Complete, Ready for Production

    The first release candidate for Entity Framework 5 -- Microsoft's object-database mapper for .NET -- has shipped with a go live license, ready for production.

Upcoming Events