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.
Oren trutner
source share