独自のコントローラーから呼び出す子アクションが1つあり、子アクションに属するビュー/共有/部分ビュー名を返します。これを別のコントローラーでもう一度繰り返すと、同じことが行われます。全て大丈夫。しかし、私はいくつかのコントローラーDRYで子アクションを繰り返します
ビューフォルダーで部分ビューを共有するように、コントローラー間で1つの子アクションを共有するにはどうすればよいですか?
私は以下を利用していますが、動作しません:
@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 />