1

我有一个像这样的基本控制器

  [Action]
  public abstract class ApplicationController : Controller
  {
     public bool HasRight { get { return ((bool)ViewData["Actions2"]); } }        
  .........
  }

动作属性

public class ActionAttribute : ActionFilterAttribute
{
   public override void OnActionExecuted(ActionExecutedContext filterContext)
   {
      filterContext.Controller.ViewData["Actions2"]=true;
      .........
   }

} 

当我从某个控制器调用某些视图时,我得到空异常

public bool HasRight { get { return ((bool)ViewData["Actions2"]); } } as ViewData is null
4

2 回答 2

4

我认为您正在寻找OnActionExecuting在您的 Controller 操作之前执行的,而不是OnActionExecuted.

这应该有效:

protected override void OnActionExecuting(ActionExecutingContext filterContext) {
    filterContext.Controller.ViewData["Actions2"]) = true;
}
于 2012-05-29T11:11:49.860 回答
1

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

于 2012-05-29T11:14:01.240 回答