問題:
必要なのは @Html.RenderAction("action","controller") に似たものですが、別のコントローラー内から使用できるものです。これは例えば:
public class FirstController : Controller
{
public ActionResult GetErDone()
{
return View();
}
}
public class SecondController : Controller
{
public ActionResult Index()
{
ActionResult coolResult =
Helper.GetActionResult("GetErDone", "FirstController");
return View();
}
}
進捗:
実際の @Html.Action() メソッドの削除/再構築を開始しましたが、実際には内部ヘルパーの依存関係が多すぎるため、これはそれほど難しくないと考えています。私がこれまでに持っているものは次のとおりです。
private void TestCreateActionFromDifferentController(RouteValueDictionary routeValues, string controllerName, string actionName)
{
var httpContext = this.HttpContext;
var routeData = this.RouteData;
routeData.Values["action"] = (object)actionName;
if (!string.IsNullOrEmpty(controllerName))
routeData.Values["controller"] = (object)controllerName;
IController myController = ControllerBuilder.Current.GetControllerFactory().CreateController(new RequestContext(httpContext, routeData) , "staticpages");
}
もちろん、これは部分的に機能しますが (エリア データ トークンなどの多くの欠点を除いて)、MyActionResult
オブジェクトに到達するためのリフレクション以外の方法はありません。
概要:
コントローラー内で、別のコントローラーで定義されたアクションから ActionResult を取得する方法は何ですか?
アップデート:
より具体的には、System.Web.Mvc.Html.ChildActionExtensions.ChildActionMvcHandler のようなものを使用しようとしていますが、実行されず、ActionResult を返します。