Child Action Method in Asp.Net Mvc 4
In this article, I’m explaining the child action method in asp.net mvc 4 and its uses.
Any public method in a controller class is an action method. Every action method in a controller class can be invoked via an URL from web browser or a view page in application.
A Child Action in ASP.Net MVC is kind of similar to that of a User Control in ASP.Net web forms. It allows for a controller to execute for a portion of the rendered area of a view, just like in Web Forms where you can execute a User Control for a portion of the rendered area of a page.
We can annotate an action method with [ChildActionOnly] attribute for creating a child action.
First create a basic asp.net mvc 4 application and a controller named “HomeController” to it:
public class HomeController : Controller
public ActionResult Index()
ViewBag.HomeAction = "Home Action Method Called";
public ActionResult ChildAction()
Now add two views named “Index” and “ChildAction” like this”
ViewBag.Title = "Index";
<h2>Child Action Method Sample</h2>
@Html.ActionLink("Child Action Method", "ChildAction", "Home")
ViewBag.Title = "ChildAction";
<h2>Child Action Rendered</h2>
Now run the application:
As you can see that the child action is rendered.
Now click on Child Action Method link: