MVC what are some practical examples when one needs to use ChildActionOnly attribute

775 views Asked by At

I have just started learning MVC and I am aware of the fact that when an action has the [ChildActionOnly] attribute, it cannot be invoked from the browser as a GET request and can only be accessed from a View.

Looking for some practical use cases where [ChildActionOnly] attribute can be used.

Linked to : Using ChildActionOnly in MVC

1

There are 1 answers

0
amitbansalite On

After reading up on this topic, I could find a good example where [ChildOnlyAction] attribute can be used.

Scenario: When one needs to display a partial view in the header/footer section of the layout page and this partial view needs to be passed a model.

Solution: Call a [ChildActionOnly] from the layout page using "@Html.Action()" which would return the partial view. In this action , one can easily pass on any model that is required by the partial view.