Is recursively rendering a Partial View in Asp.Net Mvc a bad idea?
-
06-07-2019 - |
Question
I want to output a menu structure that will look like this
<ul>
<li>
MenuItemName1
<ul>
<li>Child Item</li>
</ul>
</li>
<li>
MenuItemName2
</li>
</ul>
I have a menuitem class that contains the name, url and children menu items. I would like to create a partial view that renders each item as an unordered list like above and I would call it recursively to go through the entire tree.
Is this a bad idea? Will it be very slow?
Solution
I'm doing this and it doesn't seem particularly slow, but it's not a high volume site. Would be a great place to wire in some caching.
Check out the answers to my question on the same topic. I think the HTMLHelper extension method might perform a bit better than nested partial views.
OTHER TIPS
It is always a good idea to be lazy and save yourself some repetitive work.
It shouldn't be slower than having a view with all elements directly on it and no partial render calls.
I'm not sure about MVC but with traditional ASP.Net 2.0 if you nested user controls too deeply you could blow the stack. I saw this happen once at about 10+ levels of nesting.