简体   繁体   English

从ASP.NET MVC中的布局调用操作方法

[英]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. 所以我需要调用一个action方法来从数据库中获取菜单项。 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: 当我尝试在Web浏览器中浏览页面时,发生此错误:

The controller for path '/' was not found or does not implement IController. 未找到路径'/'的控制器或未实现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. 注意:我也尝试了Html.RenderAction,Html.Partial方法......我尝试创建另一个视图文件夹,并创建一个新的局部视图和以“文件夹名称+控制器”后缀命名的新控制器。

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: _TopMenu.cshtml:

@model IList<string>

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

LayoutController (in Controllers folder): 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);
        }    
    }

What happens if you put this in your view? 如果你把它放在你的视野中会发生什么?

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

(And comment this out until everything works: //[ChildActionOnly]) (并注意这一切,直到一切正常:// [ChildActionOnly])

Change this line, 改变这一行,

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

to, 至,

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

and check. 并检查。

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); 存在不同的方式,对于这种情况我喜欢在布局中使用html.action,并且在控件中我将创建一个字符串Menu,该字符串包含我需要的html代码,控制器结束时返回Content(菜单);

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. 我喜欢这样,因为我可以使用很多选项来创建菜单dinamics更复杂。

other way use ajax to recovered the data and use handlebars or other template for the code 另一种方法是使用ajax恢复数据并使用把手或其他模板代码

You are using the wrong overload of the Action-Method. 您正在使用Action-Method的错误重载。 The 2nd parameter in the variation is not the controllername but the actionname. 变体中的第二个参数不是控制器名称,而是actionname。

You can check the correct Method overloads on this page 您可以在此页面上检查正确的方法重载

Also: If you specify Controllers in the Html.Action Method (which you can do for example with this variation of the Method), you dont need to write the suffix "Controller" even if thats your Classname. 另外:如果在Html.Action方法中指定控制器(例如,您可以使用方法的变体),则不需要编写后缀“Controller”,即使这是您的类名。 So Instead of using the string "LayoutController" you would write simply "Layout". 因此,不要使用字符串“LayoutController”,而只需编写“Layout”。

At this point the framework is convention-based. 此时,框架是基于约定的。

This is how I did it: 我就是这样做的:

Layout 布局

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

Admin Menu Controller 管理菜单控制器

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

    return PartialView(model);
}

GetAdminMenu.cshtml 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM