asp.net-mvc MVC5 Html.RenderAction 与不同的控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19771693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC5 Html.RenderAction with different controller
提问by SamTech
I am starting with MVC5 and created first project from MVC5 Getting Started.
我从 MVC5 开始,并从MVC5 Getting Started创建了第一个项目。
Now trying with Partial Rendering and added a method in MoviesController as below
现在尝试使用部分渲染并在 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();
}
It sets Min and Max price from Movies collection into ViewBag that are later displayed at view. I am trying to render it on different views.
它将电影收藏中的最低和最高价格设置为稍后在视图中显示的 ViewBag。我试图在不同的视图上呈现它。
First i tried to render it at Views/Movies/Index.cshtmlas below
首先,我尝试将其渲染Views/Movies/Index.cshtml如下
@{Html.RenderAction("PriceRange");}
It works well there and results displayed correctly because it is using MoviesController, the same class where method PriceRangedefined.
它在那里运行良好并且结果显示正确,因为它使用MoviesController的是PriceRange定义方法的同一个类。
Then i tried to render it at Views/Hello/Index.cshtml(this view is using HelloWorldController) with following code (first passing Action name then Controller name)
然后我尝试Views/Hello/Index.cshtml使用HelloWorldController以下代码(首先传递动作名称然后控制器名称)在(此视图正在使用)呈现它
@{Html.RenderAction("PriceRange", "MoviesController");}
Here it is giving run-time error
这里给出了运行时错误
The controller for path '/HelloWorld/Index' was not found or does not implement IController.
路径“/HelloWorld/Index”的控制器未找到或未实现 IController。
Here is complete code from Views/Hello/Index.cshtml
这是来自 Views/Hello/Index.cshtml 的完整代码
@{
ViewBag.Title = "Movie List";
}
<h2>My Movie List</h2>
<p>Hello from our view template</p>
@{Html.RenderAction("PriceRange", "MoviesController");}
I found few examples through Google, they are calling RenderAction helper the same way, first passing Action name then Controller name.
我通过谷歌找到了几个例子,他们以同样的方式调用 RenderAction 助手,首先传递动作名称,然后是控制器名称。
I couldn't understand what the wrong i am doing here. Can someone point out?
我不明白我在这里做错了什么。有人可以指出吗?
回答by Charlino
It might be that you're adding the "Controller" postfix to the controller name which isn't required.
可能是您将“控制器”后缀添加到不需要的控制器名称中。
Try:
尝试:
@{Html.RenderAction("PriceRange", "Movies");}
回答by Bas Kooistra
The controller name needs to be "Movies" and not "MoviesController". Because now I believe it is looking for a controller called "MoviesControllerController".
控制器名称必须是“Movies”而不是“MoviesController”。因为现在我相信它正在寻找一个名为“MoviesControllerController”的控制器。

