MVC Helper Menu

1

I'm having a hard time putting together a Helper to mount the system menu

This is my view

@model IList<Devcompy.ClientWeb.Devcompy2.Helpers.HierarchyNode<Devcompy.Models.Entities.Segurancas.Menu>>
<!-- Navigation -->
<nav class="navbar navbar-default navbar-static-top" role="navigation" style="margin-bottom: 0">
<div class="navbar-header">
    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
    </button>
    @Html.ActionLink("Devcompy", "Index", "Home", null, new { @class = "navbar-brand" })
</div>


<ul class="nav navbar-top-links navbar-right">
    <li>
        <a href="javascript:document.getElementById('logoutForm').submit()"><i class="fa fa-sign-out fa-fw"></i> Sair</a>
    </li>
</ul>
<!-- /.navbar-top-links -->

<div class="navbar-default sidebar" role="navigation">
    <div class="sidebar-nav navbar-collapse">
        <ul class="nav" id="side-menu">
            @foreach (var model in Model)
            {
                <li>
                    @{var firstMenu = model.Entity;}
                    <a class="active" href="@Url.Action(firstMenu.Action, firstMenu.Controller, new { @area = firstMenu.Area })"><i class="fa fa-dashboard fa-fw"></i> @firstMenu.Title</a>
                </li>
                    if (model.Entity.Menus.Count > 0)
                    {
                        DropDownMenu(model.Entity.Menus.ToList());
                    }
            }
        </ul>
    </div>
</div>
<!-- /.navbar-static-side -->
</nav>
<!-- /.Navigation -->
@if (Request.IsAuthenticated)
{
   using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" }))
{
@Html.AntiForgeryToken()
}
}
@helper DropDownMenu(List<Devcompy.Models.Entities.Segurancas.Menu> menus)
{
foreach (var menu in menus)
{
    <li>
        <a href="#">@menu.Title<span class="fa arrow"></span></a>
        <ul class="nav nav-second-level">
            <li>
                <a href="flot.html">Clientes</a>
            </li>
            <li>
                <a href="morris.html">Produtos</a>
            </li>
        </ul>
        <!-- /.nav-second-level -->
    </li>
}
}

The difficulty is in the following question, when calling Helper DropDownMenu passing the list it is not entering is not arriving in the foreach of my Helper, being q I know my list has items.

Not editing my Helper to not receive the parameter it is working

    
asked by anonymous 20.12.2014 / 15:18

1 answer

2

I find this approach very complicated. You can use @Html.Partial which produces the same result with easier debugging:

@model IList<Devcompy.ClientWeb.Devcompy2.Helpers.HierarchyNode<Devcompy.Models.Entities.Segurancas.Menu>>
<!-- Navigation -->
<nav class="navbar navbar-default navbar-static-top" role="navigation" style="margin-bottom: 0">
<div class="navbar-header">
    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
    </button>
    @Html.ActionLink("Devcompy", "Index", "Home", null, new { @class = "navbar-brand" })
</div>


<ul class="nav navbar-top-links navbar-right">
    <li>
        <a href="javascript:document.getElementById('logoutForm').submit()"><i class="fa fa-sign-out fa-fw"></i> Sair</a>
    </li>
</ul>
<!-- /.navbar-top-links -->

<div class="navbar-default sidebar" role="navigation">
    <div class="sidebar-nav navbar-collapse">
        <ul class="nav" id="side-menu">
            @foreach (var model in Model)
            {
                <li>
                    @{var firstMenu = model.Entity;}
                    <a class="active" href="@Url.Action(firstMenu.Action, firstMenu.Controller, new { @area = firstMenu.Area })"><i class="fa fa-dashboard fa-fw"></i> @firstMenu.Title</a>
                </li>
                    if (model.Entity.Menus.Count > 0)
                    {
                        @Html.Partial("DropDownMenu", model.Entity.Menus.ToList());
                    }
            }
        </ul>
    </div>
</div>

Views / Shared / _DropDownMenu.cshtml

@model List<Devcompy.Models.Entities.Segurancas.Menu>
{
    foreach (var menu in Model)
    {
        <li>
            <a href="#">@menu.Title<span class="fa arrow"></span></a>
            <ul class="nav nav-second-level">
                <li>
                    <a href="flot.html">Clientes</a>
                </li>
                <li>
                    <a href="morris.html">Produtos</a>
                </li>
            </ul>
            <!-- /.nav-second-level -->
        </li>
    }
}
    
20.12.2014 / 19:29