Call a method every time a Controller is triggered Asp.net mvc

Asked

Viewed 910 times

0

In my project I have a method that creates a menu on the layout page "_Layout.cshtml", for example, in my control HomeController.cs this method remains, and in ActionResult Index() I call this method, only I have several other ActionResult who calls other Views, details page etc.. I have to call this method to create the menu in all ActionResult? there’s a way every time HomeController is triggered, regardless of the 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, I have to call the method MontaMenuPrincipal() in all ActionResult? or has some way of calling this method for all ActionResult of that Controller? without having to repeat it? it would also serve other methods that would be common for all ActionResult.

Thank you!

2 answers

1


It’s not very clear what you’re trying to do, but from what I understand, on your page layout is being called some action, you can do it this way

_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 with the name "_Menu"

Partialview _Menu:

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

Other views:

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

Explanation:

In the view layout is added the @Html.Action that every time will load the code from controller home and of action Montamenuprincipal. In the controller was changed to return a PartialView and the ViewBag and, finally, in the PartialView created, is made the raw of that ViewBag

0

Good night, Ducke.

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

So that all your Views inherit this layout contained in _Layout.cshtml automatically, you should check if the file Viewstart.cshtml ( inside the Views folder of your project) is like this:

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

If so, anything on _Layout.cshtml will be applied to all your views, no need for you to repeat code.

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

If not, put the Homecontroller here for us to take a look. : D

  • Felipe, this part I understood well, I edited my question to see if it is clearer, and thank you for the help!

Browser other questions tagged

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