asp.net-mvc – MVC5 Html.RenderAction与不同的控制器

我从MVC5开始,并从 MVC5 Getting Started创建了第一个项目。

现在尝试使用部分渲染,并在MoviesController中添加一个方法,如下所示

[ChildActionOnly]
public ActionResult PriceRange()
{
   var maxprice = db.Movies.Max(m => m.Price);
   var minprice = db.Movies.Min(m => m.Price);
   ViewBag.MaxPrice = maxprice;
   ViewBag.MinPrice = minprice;
   return PartialView();
}

它将“电影”集合中的“最小”和“最大”价格设置为后来在视图中显示的ViewBag。我试图用不同的观点来呈现。

首先,我试图在Views / Movies / Index.cshtml中呈现如下

@{Html.RenderAction("PriceRange");}

它工作得很好,结果显示正确,因为它使用MoviesController,方法PriceRange定义的相同的类。

然后我试图在Views / Hello / Index.cshtml(这个视图使用HelloWorldController)中使用以下代码(首先传递Action名称,然后是Controller name)

@{Html.RenderAction("PriceRange","MoviesController");}

这里是运行时错误

The controller for path ‘/HelloWorld/Index’ was not found or does not implement IController.

这是来自Views / Hello / Index.cshtml的完整代码

@{
    ViewBag.Title = "Movie List";
}
<h2>My Movie List</h2>
<p>Hello from our view template</p>
@{Html.RenderAction("PriceRange","MoviesController");}

我通过Google发现几个例子,他们以相同的方式调用RenderAction帮助器,首先传递Action名称,然后控制器名称

我不明白我在这里做错了什么
有人可以指出

解决方法

可能是将“Controller”后缀添加到不需要的控制器名称

尝试:

@{Html.RenderAction("PriceRange","Movies");}

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....