Search code examples
c#asp.netasp.net-mvcrenderpartial

Cache RenderPartial output


I have a Pagination user control (ascx) that I render with:

<% Html.RenderPartial("Pagination", ViewData["Pagination"]); %>

I want to show this in two places though, above and below the results for that page. It seems inefficient to render the control twice so is it possible to store the output of the render in a variable which I can then output to my hearts content without worrying?


Solution

  • The short answer is that there is no super clean way to currently do this in MVC. There are a few different ways you could approach this, but since you want to trap the return from the partial inside another view instead of a controller, I would do an HtmlHelper extension that returns the view result as a string, rather than writing directly to the response.

    See this link for further details : Render a view as a string