ControllerContext.ParentActionViewContext

Topics: ASP.NET MVC
Apr 22, 2012 at 10:02 AM

On ControllerContext class, there is a public property named ParentActionViewContext which is typeof ViewContext and its implementation is as follows:

        //...

        internal const string ParentActionViewContextToken = "ParentActionViewContext";

       //...

        public ViewContext ParentActionViewContext
        {
            get { return RouteData.DataTokens[ParentActionViewContextToken] as ViewContext; }
        }

       //...

Also, all of FilterContext classes are derived from ControllerContext class. When I inspect the action and result filters, I see that ParentActionViewContext is null all the time.

What is this property is used for?

Apr 22, 2012 at 10:09 AM
Edited Apr 22, 2012 at 11:18 AM

Ok, I now figured it out (I guess). Correct me if I am wrong:

This is used for ChildActions. If a request comes to a child action, then ParentActionViewContextToken is filled in with parent action's ViewContext information.

So, I think ParentActionViewContext property won't be always provided and accessing it is not safe unless I do a null check. 

Apr 22, 2012 at 2:26 PM
That is correct. It's non-null only when you're in a child action.