Reputation: 8199
I have a basecontroller like this
[Action]
public abstract class ApplicationController : Controller
{
public bool HasRight { get { return ((bool)ViewData["Actions2"]); } }
.........
}
Action Attribute
public class ActionAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.Controller.ViewData["Actions2"]=true;
.........
}
}
When i call some view from certain controller I get null exception at
public bool HasRight { get { return ((bool)ViewData["Actions2"]); } } as ViewData is null
Upvotes: 1
Views: 2765
Reputation: 14094
You need to get ViewData
from the FilterContext
Like this:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.Controller.ViewData["Actions2"] = true;
base.OnActionExecuting(filterContext);
}
Update :
After your update i realized that you're trying to get access to ViewData["Actions2"]
before setting the value to it.
So you should use OnActionExecuting
instead of OnActionExecuted
to ensure that the value has been set to the ViewData
Upvotes: 1
Reputation: 8079
I think you're looking for OnActionExecuting
which is executed before your Controller action and not OnActionExecuted
.
This should work:
protected override void OnActionExecuting(ActionExecutingContext filterContext) {
filterContext.Controller.ViewData["Actions2"]) = true;
}
Upvotes: 4