0

In ASP.NET MVC views, it's possible to call child actions like this:

@Html.Action("ActionName")

That will render the results of the action called ActionName to the output of the view.

Is it possible to make such child action call in the controller?


The output of my controller actions is JSON, not HTML, so I prepare the resulting JSON objects in the controllers instead of the Razor templates. It would be useful if I could use the same mechanism as I use when working with Razor views to render parts of the response by a child action.

I know that there's a possibility of structuring the code differently, so I could reuse the same component in two different controllers. But I want to specifically know if I can get the results of a child action in a controller the same way I can do in the view.

Please see the question comments below as well for more information about the problem.

Tom Pažourek
  • 9,582
  • 8
  • 66
  • 107
  • Maybe `RedirectToAction`? – diiN__________ Mar 09 '16 at 08:06
  • @diiN_: That will not work. I only want a part of the JSON object on the output to be rendered from a child action, not the whole thing. – Tom Pažourek Mar 09 '16 at 08:07
  • Something like RazorEngine (https://github.com/Antaris/RazorEngine) will let you render a razor template and return it as a string to the controller if that is what you are after? "Heavy" solution though IMO – LDJ Mar 09 '16 at 08:12
  • @LDJ: I don't want to use Razor though. If I wanted to use Razor templates, I could just use the ones that are built in and call the child action from the view by calling `@Html.Action("ActionName")`. Razor is not well suited for outputting JSON. – Tom Pažourek Mar 09 '16 at 08:15
  • What you exactly want : the HTML string of your page or you just want to call a function which gives you desired result – Varun Vasishtha Mar 09 '16 at 11:24
  • @VarunVasishtha: I am looking for the same functionality that child actions have. In the Razor templates, I can render part of the HTML output by executing a child action. I want to do the same thing. My output is one big JSON object and I want part of that object to be obtained from a child action. It's the same principle, except I am talking about JSON, not HTML. And since it's JSON, I am composing the response in controller, not in Razor view template. – Tom Pažourek Mar 09 '16 at 11:28
  • you can define another action with JsonResult and decorate it with an attribute called [NonAction] and call it in the action you want, it will give you a JSON object – Varun Vasishtha Mar 09 '16 at 11:31
  • @VarunVasishtha: However, that will only work for the action in the same controller. What if I want to call action in another controller? Also, I am looking for the exact same behavior as child actions have (which basically goes through the whole lifecycle of the child request). – Tom Pažourek Mar 09 '16 at 11:34
  • You can call in another controller by making an object of the controller in which you defined a method and not sure about your life cycle requirement. – Varun Vasishtha Mar 09 '16 at 11:42
  • @VarunVasishtha: I am afraid that creating a controller is not the correct way to do this as the controllers have dependencies and there's some lifecycle of the request (and of the child action request). The logic around it can for example allow me to use output cache attributes on the child action. The output cache attributes will not be taken into account if I just call the action like a method. – Tom Pažourek Mar 09 '16 at 11:49

0 Answers0