ChildActionOnly Attribute In ASP.NET MVC

Introduction

The action method which is decorated with the ChildActionOnly attribute is called Child Action in MVC. Child Action is only accessible by a child request. It will not respond to the URL requests. Let's understand the use of a ChildActionOnly attribute in our application.

Step 1

Open Visual Studio 2015 or an editor of your choice and create a new project.

Step 2

Choose "web application" project and give an appropriate name to your project.
 
ChildActionOnly Attribute In ASP.NET MVC 

Step 3

Select "empty" template, check on MVC checkbox below, and click OK.
 
ChildActionOnly Attribute In ASP.NET MVC 

Step 4

Right-click on the Controllers folder and add a new controller.
 
ChildActionOnly Attribute In ASP.NET MVC 

A window will appear. Choose MVC5 Controller-Empty and click "Add".

ChildActionOnly Attribute In ASP.NET MVC 

After clicking on "Add", another window will appear with a default controller. Change the name to HomeController and click "Add". The HomeController will be added under the Controllers folder. Don’t change the Controller suffix for all controllers, change only the highlight, and instead of "default", just write "Home".

The complete code for Home Controller

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.    
  7. namespace ChildActionAttribute_Demo.Controllers  
  8. {  
  9.     public class HomeController : Controller  
  10.     {  
  11.         // GET: Home  
  12.         public ActionResult Index()  
  13.         {  
  14.             return View();  
  15.         }  
  16.    
  17.         [ChildActionOnly]  
  18.         public ActionResult Students(List<string>studentList)  
  19.         {  
  20.             return View(studentList);  
  21.         }  
  22.     }  
  23. }  

Step 5

Right-click on Index method in HomeController. The "Add View" window will appear with default index name checked (use a Layout page). Click on "Add.
 
ChildActionOnly Attribute In ASP.NET MVC 

Index View

  1. @model List<string>  
  2. @{  
  3.     ViewBag.Title = "Index";  
  4. }  
  5.    
  6. <h2>List of students</h2>  
  7. @Html.Action("Students"new { studentList = new List<string>() { "Farhan Ahmed""Irfan Ahmed""Irshad Ahmed" } })  
Students View 
  1. @model List<string>  
  2. @{  
  3.     Layout = null;  
  4. }  
  5.    
  6. @foreach (var stud in Model)  
  7. {  
  8.     <ul>  
  9.         <li>  
  10.             @stud  
  11.         </li>  
  12.     </ul>  
  13. }  

Points to remember about ChildActionOnly attribute

  1. The action method which is decorated with the ChildActionOnly attribute is called a child action method.
  2. Child action methods do not respond to URL requests. If an attempt is made, a runtime error is thrown stating that Child action is accessible only by a child request.
  3. Child action methods can be invoked by making child request from a view using Action() and RenderAction() HTML helpers.
  4. An action method doesn’t need to have [ChildActionOnly] attribute to be used as a child action but uses this attribute to prevent if you want to prevent the action method from being invoked as a result of a user request.
  5. Child actions are typically associated with partial views, although this is not compulsory.
  6. Child action methods are different from NonAction methods, in that NonAction methods cannot be invoked using Action() or RenderAction() helpers.
  7. Using child action methods, it is possible to cache portions of a view. This is the main advantage of child action methods. We will cover this when we discuss the [OutputCache] attribute.

Step 6

Build and run your project by pressing ctrl+F5.

http://localhost:59591/Home/Index

ChildActionOnly Attribute In ASP.NET MVC
http://localhost:59591/Home/students
 
ChildActionOnly Attribute In ASP.NET MVC