ASP.NET MVC - for a loop inside a RenderPartial or an external RenderPartial

Which one has the best performance. I have a list containing a list of articles. When I list the articles, I have a RenderPartial that displays only one article, and the parent page goes through the cycle of all the articles. Renderpatial is inside the parent page loop. What is the best way to do this?

+3
asp.net-mvc
source share
2 answers

If you can make a loop inside a partial view. Each time you call RenderPartial, the following things happen (given the source MVC files):

  • RenderPartial calls RenderPartialInternal

  • RenderPartialInternal creates a new ViewDataDictionary and a new ViewContext

  • RenderPartialInternal calls FindPartialView to search and instantiate the view.

  • FindPartialView searches for all registered view engines (usually only one) for the presentation using the controller context and the name of the presentation as keys. Each view engine looks for representation in all the routes it supports, for example. Views / controller / view.aspx, Views / controllers / view.ascx, Views / Shared / view.aspx, etc. Views can be returned from the memory cache to speed up this step.

  • The Render method is called. I lost track of the inner workings of the Render method of the WebFormView standard 13 levels down the stack. Render creates a large number of context objects that need an internal view, checks permissions to run the view, connects events for any server controls, re-checks the Request object to decide what else needs to be done, and so on. After rendering the view, it expands the context it creates.

All in all, none of this is too bad. All this happens inside the processor and RAM of the computer, which is more than can be said about the typical access to the database that occurs in the controller. The process should only go to disk the first time the view is loaded (this may be slow, however, files must be searched, and the view must be compiled). ASP.NET MVC had to optimize the rendering process to maintain a high level of performance.

However, this is quite a bit, and if you can avoid running it several times in one request, this will help improve the response time of the action method.

+7
source share

One thing that will increase the performance of your views several times is to set Debug=false in your web.config (i.e. deployment in Release mode)

In this case, the MVC engine will cache all views (including partial ones) and will not try to resolve their location and load them every time they try to use them.

0
source share

All Articles