What is an MVC child action?

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

Can someone explain these methods?

+54
asp.net-mvc
Sep 21 '12 at 11:50
source share
4 answers

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

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

This action will then execute and display its output at the specified location in the view. The difference with Partial is that partial only includes the specified markup, there is no other action that performs the main action.

So you have the main action that received the request and displayed the view, but from this view you can perform several child actions that will go through their independent MVC life cycle and ultimately output the result. And all this will happen in the context of a single HTTP request.

Child actions are useful for creating entire reusable widgets that can be embedded in your views and that go through their independent MVC life cycle.

+92
Sep 21 '12 at 11:52
source share
— -

A child action is an action that is called using html.renderaction or html.action helper from within the view.

+7
May 29 '13 at 5:49
source share

A child action is an action method that is called in the view via @ 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 }) 
+2
Jul 12 '17 at 12:31 on
source share

Points to be noted:

  1. Any action method that is decorated with the [ChildActionOnly] attribute is a child action method.
  2. Child action methods will not respond to URL requests. If an attempt is made, a runtime error will be generated: "A child action is available only on a child request.

  3. Child action methods can be called by creating a child request from the view using the HTML helpers "Action ()" and "RenderAction ()".

  4. The action method should not have the [ChildActionOnly] attribute that will be used as a child action, but use this attribute to prevent if you want the action method not to be called as a result of a user request.

  5. Child actions are usually associated with partial representations, although this is not required.

  6. Child action methods differ from NonAction methods in that NonAction methods cannot be called using the Action () or RenderAction () helpers.

  7. Using child action methods, you can cache parts of a view. This is the main advantage of child action methods.

0
May 27 '19 at 3:17
source share



All Articles