Home Render recursive list in view using razor? (was helper method)
Reply: 0

Render recursive list in view using razor? (was helper method)

Stian
1#
Stian Published in 2017-12-06 14:00:16Z

I have read that Asp.Net Core doesn't support custom helper methods (/App_Code/MyHelpers.cshtml). Is there another way of doing this (recursive rendering of tree structured list)?:

@helper BuildMenu(IEnumerable<MenuItem> data, int? parentId = null)
{
    var items = data.Where(d => d.ParentMenuItemId == parentId).OrderBy(i => i.MenuOrder);
    if (items.Any())
    {
    <ul>
        @foreach (var item in items)
        {
            <li>
                <a href="@item.LinkUrl">@item.MenuText</a>
                @BuildMenu(data, item.MenuItemId)
            </li>
        }
    </ul>
    }
}

Can it be directly implemented into the view? If so, how?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.310943 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO