Call an action method from layout in ASP.NET MVC

23,272

Solution 1

What happens if you put this in your view?

@{ Html.RenderAction("TopMenu", "Layout"); }

(And comment this out until everything works: //[ChildActionOnly])

Solution 2

Change this line,

@Html.Action("~/Views/Shared/_TopMenu.cshtml", "LayoutController", new {area =""});

to,

@Html.Action("_TopMenu", "Layout", new {area =""});

and check.

Solution 3

exist differents ways, for this case I like use html.action in layout, and in control I will create a string Menu, the string contains the html code I need, the controller end with return Content(menu);

for example

Layout:

<body>
    <nav>
       @Html.Action("_TopMenu", "Layout")
    </nav>

the controller

   public class LayoutController : Controller
    {
        public ActionResult _TopMenu()
        {
            IList<string> menuModel = GetFromDb();
            string menu = "<ul>";
            foreach(string x in menuModel)
            {
                menu +="<li><a href='"+x+"'>+x+"</a></li>";
            }
            menu+="</ul>";
            return Content(menu);
        }
   }

I like that because I can use many options to create menus dinamics more complexes.

other way use ajax to recovered the data and use handlebars or other template for the code

Share:
23,272
Uğur Aldanmaz
Author by

Uğur Aldanmaz

Updated on May 08, 2020

Comments

  • Uğur Aldanmaz
    Uğur Aldanmaz almost 4 years

    I have one layout and one partial view which are in the Shared folder. Partial view presents top menu items which are not static. So I need to call an action method to get menu items from database. To do this, I created a controller and add an action method in it.

    When I try to browse the page in web browser, this error occured:

    The controller for path '/' was not found or does not implement IController.

    Note: I tried Html.RenderAction, Html.Partial methods too... And I tried to create another view folder, and create a new partial view and new controller that named with "folder name + Controller" suffix.

    Layout:

    <!DOCTYPE html>
    <html>
    <head>
        <title>@ViewBag.Title</title>
    </head>
    <body>
        <div id="header">
            @Html.Action("~/Views/Shared/_TopMenu.cshtml", "LayoutController", new {area =""}); //Here is the problem.
    
        </div>
       <div>
            @RenderBody();
       </div>
    
    </body>
    </html>
    

    _TopMenu.cshtml:

    @model IList<string>
    
    @foreach (string item in Model)
    {
        <span>item</span>
    }
    

    LayoutController (in Controllers folder):

     public class LayoutController : Controller
        {
            //
            // GET: /Shared/
            public ActionResult Index()
            {
                return View();
            }
    
            [ChildActionOnly]
            [ActionName("_TopMenu")]
            public ActionResult TopMenu()
            {
               IList<string> menuModel = GetFromDb();
               return PartialView("_TopMenu", menuModel);
            }    
        }