Search code examples
performanceasp.net-mvc-2loopsrenderpartial

Calling RenderPartial in a loop : Avoid when possible?


Is it generally best to avoid calling to render partial in a loop situation...

<%  foreach (var buildingRate in locationBuildingRate.BuildingRates)
    {
        Html.RenderPartial("LocationBuildingRate", buildingRate);
    }
%>

And instead allow the rendering to loop inside the partial? Does this second way avoid a lot of overhead?

Html.RenderPartial("LocationBuildingRate", locationBuildingRate.BuildingRates);

Solution

  • Yes. Calling a render partial inside a loop would request the rendering engine for each run. Better to do the second approach where you loop inside the partial..