一个孩子如何行动&从多个控制器调用它?

时间:2014-04-05 11:10:28

标签: c# asp.net-mvc child-actions

我有一个Child动作,可以通过它自己的控制器调用它。 return 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 />

0 个答案:

没有答案