MVC Helper Menu

Asked

Viewed 287 times

1

I’m having some difficulty in assembling a Helper to assemble 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 the Helper DropDownMenu passing the list he is not entering is not arriving at the foreach from my Helper, I know that my list has items.

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

  • If you debug what comes at breakpoint ?

  • The menu parameter appears with 1 item, but does not arrive in foreach

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>
    }
}
  • Blz Gypsy, I’ll test and then I’ll tell you the result

  • But I figured out why it’s not enough foreach when I call my helper. :(

Browser other questions tagged

You are not signed in. Login or sign up in order to post.