- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the use of ChildActionOnly attribute in ASP .Net MVC C#?
Child Action is only accessible by a child request. It will not respond to the URL requests. If an attempt is made, a runtime error will be thrown stating - Child action is accessible only by a child request. Child action methods can be invoked by making child request from a view using Action() and RenderAction() html helpers.
Child action methods are different from NonAction methods, in that NonAction methods cannot be invoked using Action() or RenderAction() helpers.
Below is the Child Action Error when we try to invoke it using URL.
Controller
Example
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> }
Output
Child actions can also be invoked using "RenderAction()" HTML helper as shown below.
@{ Html.RenderAction("Countries", new { countryData = new List<string>() { "USA", "UK", "India", "Australia" } }); }
- Related Articles
- What is ViewData in ASP .Net MVC C#?
- How to use ViewBag in ASP .Net MVC C#?
- What is the significance of NonActionAttribute in ASP .Net MVC C#?
- What are the three segments of the default route, that is present in ASP .Net MVC C#?
- What are the levels at which filters can be applied in ASP .Net MVC C#?
- How can we provide an alias name for an action method in Asp .Net MVC C#?
- What is the use of Authorize Attribute in C# Asp.Net webAPI?
- What is serialization in C#.NET?
- What is the formula of net displacement?
- What is the base class for all data types in C#.NET?
- What is the use of "is" keyword in C#?
- What is the use of cin.ignore() in C++?
- What are the different access specifiers in C#.NET?
- What is Net Concept of Working Capital?
- What are access specifiers in C#.NET?

Advertisements