ASP.NET MVC ActionFilter参数绑定

时间:2009-12-25 16:11:42

标签: asp.net-mvc action-filter

如果动作方法中有模型绑定参数,如何在动作过滤器中获取该参数?

[MyActionFilter]
public ActionResult Edit(Car myCar)
{
    ...
}

public class MyActionFilterAttribute : ActionFilterAttribute
{
    public void OnActionExecuted(ActionExecutedContext filterContext)
    {
        //I want to access myCar here
    }

}

无论如何都没有通过Form变量来获取myCar吗?

1 个答案:

答案 0 :(得分:11)

不确定OnActionExecuted但您可以在OnActionExecuting中执行此操作:

public class MyActionFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // I want to access myCar here

        if(filterContext.ActionParameters.ContainsKey("myCar"))
        {
            var myCar = filterContext.ActionParameters["myCar"] as Car;

            if(myCar != null)
            {
                // You can access myCar here
            }
        }
    }
}