2

Problem:

What I need is something similar to @Html.RenderAction("action","controller") , but one that I can use from inside a different controller. This for example:

public class FirstController : Controller
{
   public ActionResult GetErDone()
   {
        return View();
   }
}

public class SecondController : Controller
{
   public ActionResult Index()
   {
        ActionResult coolResult = 
               Helper.GetActionResult("GetErDone", "FirstController");

        return View();
   }
}

Progress:

I have begun to strip/reform the actual @Html.Action() method but it really has too many internal helper dependencies and I am thinking this shouldn't be that tough. What I have so far is:

    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");
    }

This works partially of course (minus the many shortcomings like area data tokens etc) but still there is no way other than reflection to get to the MyActionResult object.

Summary:

When inside a controller, what is a way to get an ActionResult from an action that is defined on a different controller, ?

Update:

More specifically, I am trying to use something like System.Web.Mvc.Html.ChildActionExtensions.ChildActionMvcHandler but one that doesnt execute, rather it returns the ActionResult

Mihalis Bagos
  • 2,500
  • 1
  • 22
  • 32
  • http://stackoverflow.com/questions/483091/render-a-view-as-a-string – Parv Sharma May 30 '12 at 12:34
  • possible duplicate of [.NET MVC Call method on different controller](http://stackoverflow.com/questions/1296680/net-mvc-call-method-on-different-controller) – jgauffin May 30 '12 at 12:36
  • 1
    Thanks for the links, however: I dont need to `Render` a `View`, I need to create an instance of an `ActionResult` object (more similar to this is to render an Action - Still different from rendering a View). Also, I dont need to return a string, I need to return an ActionResult (which needs the controllerContext thus it cannot be refactored). This is very specific and very different – Mihalis Bagos May 30 '12 at 13:04
  • what exactly are you trying to accomplish with this? Are you trying to make a Mash up of multiple actions? – JasonD May 30 '12 at 19:13
  • Main design reason is I want to manage dependencies at runtime. Imagine a dependency resolution tier that sits right before presentation tier (before sending to views) – Mihalis Bagos May 31 '12 at 08:57

1 Answers1

1

Not sure why you want to return a ActionResult from a another controller. Possibly you could provide more details to understand us problem domain.

I think You could consider to use BaseController and declare a Static Method GetErrorDone with return results.

This method can be called from All Child Controllers.


OR 2nd Answer

Consider to just Redirect it to required Action using Controller.RedirectToAction

e.g. RedirectToAction("action name" "controller Name")
swapneel
  • 3,061
  • 1
  • 25
  • 32
  • Thanks for the reply, however a shared method doesnt help my case. I need to create many parts then compose them. – Mihalis Bagos May 30 '12 at 14:09
  • An action result represents a "command" that the framework will perform on behalf of the action method. What is your command? What it will return back to View? – swapneel May 30 '12 at 14:29
  • This is exactly the problem - I dont want the ActionResult to return directly to View, I want the ActionResult to return to Controller. I just want to move the responsibility of gathering all the child ActionResult , at the controller level, not the View level. iE: Instead of (At razor) @Html.Action([get main menu]), @Html.Action([get side bar]) etc.... I want to get all the components at a main Controller – Mihalis Bagos May 30 '12 at 15:08