What is the use of ChildActionOnly attribute in ASP .Net MVC C#?

The ChildActionOnly attribute in ASP.NET MVC C# restricts an action method to be accessible only through child requests from views using Html.Action() or Html.RenderAction() helpers. It prevents direct URL access to the action method, making it ideal for creating reusable partial components.

Syntax

Following is the syntax for using the ChildActionOnly attribute −

[ChildActionOnly]
public ActionResult ActionName() {
   // action logic
   return View();
}

To invoke a child action from a view, use one of these helper methods −

@Html.Action("ActionName", new { parameter = value })
@{ Html.RenderAction("ActionName", new { parameter = value }); }

Key Differences

ChildActionOnly NonAction
Can be invoked using Html.Action() or Html.RenderAction() Cannot be invoked by HTML helpers
Blocked from direct URL access Completely excluded from MVC routing
Used for reusable view components Used for private helper methods

Using ChildActionOnly Attribute

Below is a complete example demonstrating the ChildActionOnly attribute with a countries list component −

Controller

using System.Collections.Generic;
using System.Web.Mvc;

namespace DemoMvcApplication.Controllers {
   public class HomeController : Controller {
      public ActionResult Index() {
         return View();
      }

      [ChildActionOnly]
      public ActionResult Countries(List<string> countries) {
         return View(countries);
      }
   }
}

Index View

@{
   ViewBag.Title = "Countries List";
}
<h2>Countries List</h2>
@Html.Action("Countries", new { countries = new List<string>() { "USA", "UK", "India", "Australia" } })

Countries View

@model List<string>
@foreach (string country in Model) {
   <ul>
      <li>
         <b>
            @country
         </b>
      </li>
   </ul>
}

The output will display the countries list properly when accessed through the Index view. However, if you try to access the Countries action directly via URL (e.g., /Home/Countries), you will get a runtime error.

Child Action Error when accessed via URL

The successful output when accessed through the parent view −

Countries List Output

Using RenderAction() Helper

Child actions can also be invoked using RenderAction() HTML helper, which renders the output directly to the response stream −

@{
   Html.RenderAction("Countries", new { countries = new List<string>() {
   "USA", "UK", "India", "Australia" } });
}

Common Use Cases

  • Navigation menus that appear on multiple pages

  • Sidebar widgets with dynamic content

  • User profile summaries displayed across different views

  • Shopping cart components shown on various pages

Conclusion

The ChildActionOnly attribute in ASP.NET MVC restricts action methods to be accessible only through child requests, preventing direct URL access. This makes it perfect for creating reusable view components that can be embedded in multiple views using Html.Action() or Html.RenderAction() helpers.

Updated on: 2026-03-17T07:04:36+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements