我有一个子操作从它自己的控制器调用它并返回 Views/Shared/PartialViewName 属于我的子操作。再次在另一个控制器中重复此操作并执行相同操作。一切都好。但我在某些控制器中重复子动作DRY
如何在控制器之间共享一个子操作是否可以像在视图文件夹中共享部分视图一样?
我在下面使用但不起作用:
@Html.Action("actionName","ControllerName")
更新: 我不使用区域
这是我的代码:
public class ItemCatController : Controller
{
[ChildActionOnly]
public ActionResult PopulateTree() { return View("Tree", GetTree()); ... lots of other code }
}
返回视图:
<h2>ItemCat Index</h2>
<hr />
<button onclick="New()">new</button>
<br />
@Html.Action("PopulateTree")
和这个:
public class ItemController : Controller
{
*// repeat these codes again, this is aginst DRY principle.*
[ChildActionOnly]
public ActionResult PopulateTree() { return View("Tree", GetTree()); ... lots of other code }
}
返回这个:
<h2>Index Item</h2>
<hr />
@Html.Action("PopulateTree")
<br />