What is an MVC child action?

asp.net Mvc

asp.net Mvc Problem Overview


I read about child actions in MVC (fundamental book), but I don't really know what it is?

Could some one please explain these methods?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

Phil Haack explains it nicely in this blog post. Basically a child action is a controller action that you could invoke from the view using the Html.Action helper:

@Html.Action("SomeActionName", "SomeController")

This action will then execute and render its output at the specified location in the view. The difference with a Partial is that a partial only includes the specified markup, there's no other action executing than the main action.

So you basically have the main action which received the request and rendered a view, but from within this view you could render multiple child actions which will go through their independent MVC lifecycle and eventually render the output. And all this will happen in the context of a single HTTP request.

Child actions are useful for creating entire reusable widgets which could be embedded into your views and which go through their independent MVC lifecycle.

Solution 2 - asp.net Mvc

A child action is an action that is invoked by using html.renderaction or html.action helper from inside of a view.

Solution 3 - asp.net Mvc

A child action is an action method that is invoked in the view through @Html.Action().

> Example I have an Action on my controller.

public DateTime Time(DateTime time)
{
    return time;
}

To call this action from the View i will use:

@Html.Action("Time", new { time = DateTime.Now }) 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShahrooz JafariView Question on Stackoverflow
Solution 1 - asp.net MvcDarin DimitrovView Answer on Stackoverflow
Solution 2 - asp.net MvcKumar ManishView Answer on Stackoverflow
Solution 3 - asp.net MvcyogihostingView Answer on Stackoverflow