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

  • Hands On: New VS Code Insiders Build Creates Web Page from Image in Seconds

    New Vision support with GitHub Copilot in the latest Visual Studio Code Insiders build takes a user-supplied mockup image and creates a web page from it in seconds, handling all the HTML and CSS.

  • Naive Bayes Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the naive Bayes regression technique, where the goal is to predict a single numeric value. Compared to other machine learning regression techniques, naive Bayes regression is usually less accurate, but is simple, easy to implement and customize, works on both large and small datasets, is highly interpretable, and doesn't require tuning any hyperparameters.

  • VS Code Copilot Previews New GPT-4o AI Code Completion Model

    The 4o upgrade includes additional training on more than 275,000 high-quality public repositories in over 30 popular programming languages, said Microsoft-owned GitHub, which created the original "AI pair programmer" years ago.

  • Microsoft's Rust Embrace Continues with Azure SDK Beta

    "Rust's strong type system and ownership model help prevent common programming errors such as null pointer dereferencing and buffer overflows, leading to more secure and stable code."

  • Xcode IDE from Microsoft Archrival Apple Gets Copilot AI

    Just after expanding the reach of its Copilot AI coding assistant to the open-source Eclipse IDE, Microsoft showcased how it's going even further, providing details about a preview version for the Xcode IDE from archrival Apple.

Subscribe on YouTube

Upcoming Training Events