Any action method that is decorated with ChildActionOnly
attribute is a child action method
Child action methods will not respond to 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.
An action method doesn’t need o have childactiononly
attribute to be used as a child action, but use this attribute to be used as a
child action, but use this attribute to prevent a action method from being
invoked as a result of user request.
Child actions are typically associated with partial views, but
this is not compulsory.
Child action methods are different from Non Action methods,
in that non action methods cannot be invoked using Action() or RenderAction()
helpers.
Here i will explain about ChildActionOnly Attribute
Create one sample controller in your mvc application,write two action methods name it as index and childaction methods and add respective views .
public ActionResult Index()
{
ViewBag.TempValue = "Index action called at Sample controller";
return View();
}
[ChildActionOnly]
public ActionResult ChildAction(string param)
{
ViewBag.Message = "Child Action Called"+param;
return View();
}
suppose if we call Childaction method through url you will get an error.
Child action methods will not respond to 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 will not respond to URL requests. If an attempt is made, a runtime error will be thrown stating –child action is accessible only by a child request.
Now we need to call the childaction method from index view
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<h3>@ViewBag.TempValue </h3>
@Html.Action("ChildAction", "Sample", new {param=" first time" })
Here we are passing one parameter ,so that we will get this in index view.
let's see childaction view
@{
ViewBag.Title = "ChildAction";
}
<h2>ChildAction</h2>
<h3>@ViewBag.Message</h3>
now call the index action method in sample controller
the child action view will see with in the index view
Using child action methods, it is possible to cache portions of a view. This is main advantage of child action methods
tags:child action attribute in mvc 5,child action attribute in mvc 6,child action attribute in mvc 4,action filters in mvc,
filters mvc5,filters in mvc 4,filters in mvc6,child action filter in mvc,child action method,action method in mvc 5,action methods in mvc 5,action methods in mvc 6