Dynamic menu Asp.net c # Option does not appear

0

I have the following menu in a MVC 5 app

in the _Layout.cshtml view

<div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li>@Html.ActionLink("Inicio", "Index", "Home")</li>
                <li>@Html.ActionLink("Acerca de", "About", "Home")</li>
                <li>@Html.ActionLink("Contacto", "Contact", "Home")</li>

                @*Obtiene el menu a traves de Ajax*@
                <div id="MenuDinamico"></div>

            </ul>
            @Html.Partial("_LoginPartial")
        </div>

and its corresponding script in the same file

    <script>

    $(document).ready(function () {
        getMenuUser(1);
    });

    function getMenuUser(UsuarioId) {
        var parametrosAjax = {
            "UsuarioId": UsuarioId
        };
        $.ajax({
            type: 'POST',
            url: '@Url.Action("GetMenuUsuario", "Menu")',
            data: parametrosAjax,
            dataType: 'json',
            contentType: 'application/json',
            async: false,
            success: function (Data) {
                if (Data.length > 0) {
                    var MenuUsuario = ''
                    $.each(Data, function (idx, OpcMenu) {
                        MenuUsuario += OpcMenu;
                    })
                    $('#MenuDinamico').append(MenuUsuario);
                }
            },
            error: function (xhr) {
                console.log(xhr.responseText);
            },
            complete: function () {

            }
        });
    }



</script>

in the controller I have the following code:

        private List<string> _GetMenuUsuario(int UsuarioId)
    {
        // para test
        List<OpcionMenu> lstMenuUsuario = new List<OpcionMenu>();

        OpcionMenu opcMenu = new OpcionMenu();
        opcMenu.Id = 1;
        opcMenu.Nombre = "Cierre Mes";
        opcMenu.RolId = 1;
        opcMenu.PadreId = 0;
        opcMenu.AreaId = 1;
        opcMenu.AreaName = "Kam";
        opcMenu.ActionName = "Inicio";
        opcMenu.ControllerName = "Cierre";
        opcMenu.Visible = true;
        opcMenu.Activo = true;
        lstMenuUsuario.Add(opcMenu);


        List<string> lstOpcionesMenu = new List<string>();
        foreach (var OpcionMenu in lstMenuUsuario)
        {

            string LineaMenu = "<li> @Html.ActionLink('";
            LineaMenu += OpcionMenu.Nombre.ToString().Trim();
            LineaMenu += "', '";
            LineaMenu += OpcionMenu.ActionName.ToString().Trim();
            LineaMenu += "', '";
            LineaMenu += OpcionMenu.ControllerName.ToString().Trim();
            LineaMenu += "', new { area = '";
            LineaMenu += OpcionMenu.AreaName.ToString().Trim();
            LineaMenu += "' }, null) </li>";

            lstOpcionesMenu.Add(LineaMenu);
        }



        return lstOpcionesMenu;
    }

The problem is that the option does not appear in the menu even though the actionLink is correctly written.

I'm missing something?

I add an image of how the result appears on the page:

Greetings, and thanks for reading.

    
asked by Luis Gabriel Fabres 26.11.2018 в 20:23
source

1 answer

0

Html.ActionLink only work when they are compiled by the Razor which is the compiler of the views for asp.net. It will not work for you by adding the string that you return to the DOM using javascript.

Change Html.ActionLink with a <a></a> element so that the DOM can parsed and display it correctly, for example:

 string LineaMenu = "<li> <a href='google.com'>";
            LineaMenu += "Ir a google";
            LineaMenu += "</a></li>";
    
answered by 27.11.2018 / 14:53
source