Recursion for Dynamic Menu in 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.


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,

        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) =>
            @foreach (var cat in cats)
                    @if (cat.Child != null && cat.Child.Any())

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

Read more here:

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: