Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call an action method from layout in ASP.NET MVC

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);
        }    
    }
like image 835
Uğur Aldanmaz Avatar asked Oct 29 '14 12:10

Uğur Aldanmaz


3 Answers

What happens if you put this in your view?

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

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

like image 88
Richard Avatar answered Sep 19 '22 02:09

Richard


Change this line,

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

to,

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

and check.

like image 27
Venkatesh Avatar answered Sep 22 '22 02:09

Venkatesh


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

like image 45
chefjuanpi Avatar answered Sep 19 '22 02:09

chefjuanpi