Привязка параметров ASP.NET MVC ActionFilter

Если у Вас есть ограниченный моделью параметр в методе действия, как можно добраться до того параметра в фильтре действия?

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

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

}

Там должен так или иначе получить myCar, не проходя переменные Формы?

13
задан Shlomo 25 December 2009 в 17:05
поделиться

1 ответ

Не уверен насчет 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
            }
        }
    }
}
11
ответ дан 2 December 2019 в 01:10
поделиться
Другие вопросы по тегам:

Похожие вопросы: