Recursion for Dynamic Menu in Asp.net core mvc using partial View

I want to display dynamic menu with partial view using recursion. But I didn't found any solution for that. I had this code.

Contoller

public IActionResult GetMenu()
    {
        var catAll = _categoryservice.GetCategories();

        var model = catAll.Where(category => category.ParentCategoryId == null).Select(x => new ViewModel
        {
            Id = x.Id,
            Name = x.Name,
            ParentCategoryId = x.ParentCategoryId,
            

        }).ToList();
        return PartialView("~/Views/Shared/Partial/_GetMenu.cshtml", model);
    }

I want to call Controller ActionMethod in Layout Page for dynamic menu. I used RenderAction but that's not working.

_GetMenu.cshtml(Partial View)

       @{ Func<dynamic, IHtmlContent> 
        ShowMenu(IEnumerable<SarvamCart.Domain.Models.ViewModel> cats) =>
        @<ul>
            @foreach (var cat in cats)
            {
                <li>
                    @cat.Name
                    @if (cat.Child != null && cat.Child.Any())
                    {
                        @ShowMenu(cat.Child)
                    }
                </li>
            }
        </ul>;
    }

I passed this Recursion code in partial but this is also not working. Please Help me for this.



Read more here: https://stackoverflow.com/questions/67411952/recursion-for-dynamic-menu-in-asp-net-core-mvc-using-partial-view

Content Attribution

This content was originally published by Bhavna Soni at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: