我从 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();
}
它将电影收藏中的最低和最高价格设置为稍后在 View 中显示的 ViewBag。我试图在不同的 View 上呈现它。
首先我尝试在
Views/Movies/Index.cshtml
渲染它如下@{Html.RenderAction("PriceRange");}
它在那里运行良好并且结果显示正确,因为它使用的是
MoviesController
,同一个类 where 方法 PriceRange
定义。然后我尝试在
Views/Hello/Index.cshtml
渲染它(此 View 使用 HelloWorldController
)和以下代码(首先传递 Action 名称,然后是 Controller 名称)@{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");}
我通过谷歌找到了几个例子,他们以同样的方式调用 RenderAction 助手,首先传递 Action 名称,然后是 Controller 名称。
我不明白我在这里做错了什么。
有人可以指出吗?
最佳答案
可能是您将“ Controller ”后缀添加到不需要的 Controller 名称中。
尝试:
@{Html.RenderAction("PriceRange", "Movies");}
关于asp.net-mvc - MVC5 Html.RenderAction 与不同的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19771693/