1

I thought using @Html.Partial calls other views on the server side, BEFORE passing the page to the user's browser...not asynchronously.

I have some very tiny views that are called in many views, example:

Logo.cshtml:

<div ="logo">
    <h2>This is my logo</h2>
    <img src="logo.jpg"/>
</div>

Wouldn't it be inefficient to create a async GET request just for that small partial? Is there a way to reuse html server side, before passing the rendered page to the user?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Alex
  • 852
  • 1
  • 10
  • 21
  • 1
    Maybe this will help?http://stackoverflow.com/questions/5248183/html-partial-vs-html-renderpartial-html-action-vs-html-renderaction – Mark Mar 10 '14 at 18:28
  • You thought correctly. Perhaps you are confusing it with Html.Action? See above comment... – bhamlin Mar 10 '14 at 18:34

1 Answers1

3

Your question implies it's possible to call views(partials) AFTER the page is sent to the client (a single request here, not ajax), which is not possible.

Html.Partial(), Html.RenderPartial(), Html.RenderAction() and Html.Action() are executed DURING the streamwriter (networkwriter?) is streaming content to the client. All four are NOT async (as even if they were called via async methods, the rendering is still on hold until the calls are complete, nullifying the point of async). The difference between Render and a Non-Render action is how the content is delivered to the stream..

As async is not an option, You're best options are:

For each view use Html.RenderPartial().

Or for a more complex semi-nasty, barely better performance, call Html.Partial(), store it (HttpApplication... yeesh), and use that as a cache. (Really, just don't do this, it will make the code terrible to maintain).

Community
  • 1
  • 1
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • I use partials with [jQuery Templates](https://github.com/BorisMoore/jquery-tmpl) as they generate the template and then from the "main" page I load them async ;) – balexandre Mar 10 '14 at 18:57
  • As I mentioned, his question is not about Ajax as the OP states `reuse html server side, before passing the rendered page to the user`. – Erik Philips Mar 10 '14 at 18:59
  • Someone told me that partials are cached. This made me think they were called from the client, maybe they meant partials called through an aysnc method? But how can you call @Html.Partial() through an aysnc method? Do you mean calling an Action, which in turn calls @Html.Partial? – Alex Mar 11 '14 at 18:59
  • Great comment however... [You cannot Cache Partials](http://stackoverflow.com/a/4750602/209259). Actions sure, but with such a small html fragment, I personally wouldn't spin up a new HttpContext (which include security for the output cache) just to find a cached `Action()` that is tiny. – Erik Philips Mar 11 '14 at 19:08