Home/MVC/Child Action Attribute in ASP.NET MVC 4

Child Action Attribute in ASP.NET MVC 4

What is ChildActionOnly?

The ChildActionOnly attribute ensures that an action method can be called only as a child method from within a view. In simple, ChildActionOnly attribute make controller action method to consider as non-action methods (It treated as normal method).

Where to use ChildActionOnly?

  • ChildActionOnly it related with partial views but it is not compulsory. If decided not to access through url then we may declare action method as ChildActionOnly.
  • This attribute used to prevent the action method being invoked as a result of a user request, that is it prevent from accessing via url.

Creating a Child Action Method

Let we discuss with example,

public class ChildActionTestingController : Controller

public ActionResult GetUserDetails(int userId)
//Your Logic Here !!!
return PartialView(“GetUserDetails”, null);

Here GetUserDetails method is non-action method. The GetUserDetails method cant able to access by URL like ChildActionTesting/GetUserDetails.

@Html.Action(“GetUserDetails”, “ChildActionTesting”)

After declare GetUserDetails as childactiononly attribute, then it can’t allow to access through url. If you try to access, it show the error msg like below. It will treat as a normal method.

“The action ‘GetUserDetails’ is accessible only by a child request.”

Leave a Reply