Call functions from a controller in the ASP.NET MVC5 _Layout.cshtml view

2

I have a menu being loaded into an Action from a controller. I already configured the view of this action in _ViewStart to be the default of my project. But this view is not pulling any information from the comtroller. I have already tried by ViewBag, by Model and now I am trying for Session.

Pages that are configured to be the default Layout, can not receive actions from the controllers?

Follow my controller:

public class MenuController : Controller
{
    // GET: Menu
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Teste()
    {
        MenuDAO dao = new MenuDAO();
        string usuarioLogado = "diego";
        IList<Menu> menu = dao.MontaMenu(usuarioLogado);

        //ViewBag.Menu = menu;

        Session["teste"] = "Essa frase está vindo do controller";

        return View();
    }
}

Follow my view:

@{
    Layout = null;
}


@model IList<PortalAraguaina.Models.Menu>

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Teste</title>
</head>
<body>
    <div>
        Teste
        <p class="essa-eh-a-classe-de-teste">@Session["teste"]</p>
    </div>

    <div>@RenderBody()</div>
    
asked by anonymous 20.10.2017 / 18:32

1 answer

1

To render the result of an action from a view (in this case _Layout), use the Action helper.

You do not need ViewBag or any other means of data transport.

Return the view html directly: @Html.Action("Teste", "Menu");

    
28.10.2017 / 04:11