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

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube