Call a method every time a Controller fires asp.net mvc

0

In my project I have a method that creates a menu in the Layout.cshtml layout page, for example, in my control HomeController.cs is this method, and in ActionResult Index() I call this method, only I have several other ActionResult that calls other Views , details page etc .. I have to call this method to create the menu in all ActionResult ? does not have a way every time HomeController is triggered, regardless of ActionResult , can I call this method to create the menus? as if it were a Page_Load?

Ex:

In my _Layout.cshtml I have the following:

<ul class="rd-navbar-top-links list-unstyled">
   @Html.Raw(ViewBag.Menu)
</ul>

In my "HomeController.cs" I have the following

public ActionResult Index()
{
  MontaMenuPrincipal();
  return View();
}

public ActionResult OutroMetodo()
{
  MontaMenuPrincipal();
  return View();
}

public ActionResult OutroMetodo2()
{
  MontaMenuPrincipal();
  return View();
}

public void MontaMenuPrincipal()
{
   ViewBag.Menu = "html com o menu";
}

My question is, do I have to call the MontaMenuPrincipal() method on all ActionResult ? or do you have some way to call this method for all% of this Controller? without having to repeat it? this would also serve for other methods that would be common for all ActionResult .

Thank you!

    
asked by anonymous 27.05.2018 / 19:48

2 answers

1

It is not very clear what you are trying to do, but from what I understand, on your page layout is being called some action , you can do it as follows

_Layout.cshtml:

<ul class="rd-navbar-top-links list-unstyled">
    @Html.Action("MontaMenuPrincipal", "Home");
</ul>

Controller:

public ActionResult Index()
{
  return View();
}

public ActionResult OutroMetodo()
{
  return View();
}

public ActionResult OutroMetodo2()
{
  return View();
}

public ActionResult MontaMenuPrincipal()
{
   ViewBag.Menu = "html com o menu";

   return PartialView("_Menu");
}

Create a PartialView named "_Menu"

PartialView _Menu:

<div>
   @Html.Raw(ViewBag.Menu)
</div>

Other views:

@{
   Layout = "~/Views/Shared/_Layout.cshtml";
 }

Explanation:

In% with%% with% is added% with% that will every time load% code with% home and view MontaMenuPrincipal . No layout has been changed to return a @Html.Action and controller , and finally, in action created, controller of that PartialView is done

    
28.05.2018 / 18:28
0

Good evening, Ducke.

The _Layout.cshtml file is the default Layout for your views. When you create a project in Visual Studio as Asp.Net MVC, it automatically creates this file. Usually, it contains the navigation bar at the top of the page, with a few links like Home, About and Contact.

In order for all of your Views to inherit this layout contained in _Layout.cshtml automatically, you should check that the ViewStart.cshtml which is within the Views folder of your project) looks like this:

@{
   Layout = "~/Views/Shared/_Layout.cshtml";
 }

If it is, everything in _Layout.cshtml will be applied to all your views, with no need to repeat your code.

If possible, try to put this menu in _Layout.cshtml to share for all your views.

If it does not resolve, please put HomeController here so we can take a look. : D

    
28.05.2018 / 01:35