Call an action method from a layout in ASP.NET MVC

I have one layout and one partial view that are in the shared folder. In a partial view, top menu items that are not static are presented. Therefore, I need to call an action method to get menu items from the database. To do this, I created a controller and added an action method to it.

When I try to view the page in a web browser, this error occurred:

Controller for path '/' not found or does not implement IController.

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

Markup:

<!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 ( Controllers):

 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);
        }    
    }
+4
5

, ?

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

( , ://[ChildActionOnly])

+5

,

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

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

.

+3

, html.action , Menu, html, , Content (menu);

:

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

   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);
        }
   }

, .

ajax

+1

Action-Method. - , .

: Controllers Html.Action( , , ), "", . , "LayoutController" "".

.

0

:

@Html.Action("GetAdminMenu", "AdminMenu")

public PartialViewResult GetAdminMenu()
{
    var model = new AdminMenuViewModel();

    return PartialView(model);
}

GetAdminMenu.cshtml

@model ReportingPortal.Models.AdminMenuViewModel


<div class="form-group">
    <label class="col-md-4 control-label" for="selectbasic">School Name</label>
    <div class="col-md-8">
        @Html.DropDownListFor(model => model.SelectedID, new SelectList(Model.DataList, "Value", "Text", Model.SelectedID), "", new { @class = "form-control", @required = "*" })
    </div>
</div>
0

All Articles