gpt4 book ai didi

c# - 路径的 Controller ...未找到或未实现 IController

转载 作者:太空宇宙 更新时间:2023-11-03 15:17:20 25 4
gpt4 key购买 nike

我正在使用 C# 使用 ASP.NET MVC 5 编写应用程序。我需要在应用程序的右上角添加一个全局菜单。我被告知other SO使用具有 ChildActionOnly 属性的操作。

这就是我所做的。

我创建了一个这样的 BaseController

public class BaseController : Controller
{

[ChildActionOnly]
public ActionResult ClientsMenu()
{
using (SomeContext db = new SomeContext())
{
return PartialView(db.Database.SqlQuery<Client>("SELECT * FROM clients").ToList());
}
}

}

然后我像这样从 BaseController 继承了我所有的 Controller

public class TasksController : BaseController
{

public ActionResult Index(int ClientId)
{
...
return View();
}

public ActionResult Show(int SurveyId)
{
...
return View();
}

}

为了在我的布局中呈现 ClientsMenu,我添加了以下代码

@Html.Action("ClientsMenu", "Menus")

现在,当我运行我的应用程序时,出现以下错误

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

当我从布局中删除 @Html.Action("ClientsMenu", "Menus") 时,一切正常,但全局菜单当然不显示。

我该怎么做才能解决这个问题?

已更新这是我从下面的评论中得到反馈后所做的

public class TasksController : Controller
{
[ChildActionOnly]
public ActionResult ClientsMenu()
{
using (SomeContext db = new SomeContext())
{
return PartialView(db.Database.SqlQuery<Client>("SELECT * FROM clients").ToList());
}
}

public ActionResult Index(int ClientId)
{
...
return View();
}

public ActionResult Show(int SurveyId)
{
...
return View();
}

}

还是一样的错误

最佳答案

BaseController 中取出 ClientMenus Action 并将其放入自己的 Controller MenusController 中。然后您可以从您的 View 中调用该 Controller 。

@Html.Action("ClientsMenu", "Menus")

在您的示例中,您没有 MenusContoller@Html.Action("ClientsMenu", "Menus") 正在寻找。

Phil Haacked - Html.RenderAction and Html.Action另一篇文章链接到的文章为您提供了一个很好的例子。

关于c# - 路径的 Controller ...未找到或未实现 IController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38532446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com