C# Asp.net mvc - 从自定义操作过滤器访问视图模型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/872796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:59:14  来源:igfitidea点击:

Asp.net mvc - Accessing view Model from a custom Action filter

c#asp.net-mvccustom-action-filter

提问by ebrown

I am trying to access the Model data passed to the view in the action filter OnActionExecuted. Does anyone know if this is possible?

我正在尝试访问传递给操作过滤器 OnActionExecuted 中的视图的模型数据。有谁知道这是否可能?

I am trying to do something like this:

我正在尝试做这样的事情:

public override void OnActionExecuted(ActionExecutedContext filterContext)
{
    //get model data
    //...

    sitemap.SetCurrentNode(model.Name);
}

Any advice?

有什么建议吗?

采纳答案by Steven Lyons

The model is at:

该模型位于:

filterContext.Controller.ViewData.Model

回答by gustavocs

I don't know why but filterContext.Controller.ViewData.Modelis always null even when the model bind is executed before OnActionExecuted. I found a solution using the OnModelUpdatedevent to set that property before.

我不知道为什么但filterContext.Controller.ViewData.Model即使模型绑定在OnActionExecuted. 我之前找到了使用OnModelUpdated事件设置该属性的解决方案。

I have the model binder:

我有模型活页夹:

public class CustomModelBinder: DefaultModelBinder
{
    protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        controllerContext.Controller.ViewData.Model = bindingContext.Model;
        base.OnModelUpdated(controllerContext, bindingContext);
    }
}

After that you need to set the default binder to your new model binder in Application_Start()section in Global.asax:

之后,您需要Application_Start()在 Global.asax 的部分中将默认绑定器设置为新模型绑定器:

ModelBinders.Binders.DefaultBinder = new CustomModelBinder();

Finally you can access your Modelin an ActionFilterAttribute:

最后你可以访问你ModelActionFilterAttribute

public class TraceLog : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        //filterContext.Controller.ViewData.Model now isn't null
        base.OnActionExecuted(filterContext);
    }
}

回答by Matas Vaitkevicius

If you are getting null - as alternative to @Gustavo Clemente's answer you can try overriding OnActionExecutedand passing your viewModel into view in following way:

如果您获得 null - 作为@Gustavo Clemente 的答案的替代方案,您可以尝试OnActionExecuted通过以下方式覆盖您的 viewModel 并将其传递到视图中:

Action:

行动:

[Breadcrumb("Index")]
public ActionResult UnitIndex()
{
    View(new Answers());
}

Attribute:

属性:

public class BreadcrumbAttribute : ActionFilterAttribute
{
    public string Page { get; set; }

    public BreadcrumbAttribute(string page)
    {
        Page = page;
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        var model = (IBreadcrumbs)filterContext.Controller.ViewData.Model;
        model.Breadcrumbs = BreadcrumbHelper.GetBreadCrumbs(string.Format("{0}", filterContext.RouteData.DataTokens["area"]), Page);
    }
}

enter image description here

在此处输入图片说明

回答by Safin Ahmed

In .Net Core you have an ActionArguments IDictionary on the context, with all the parameters from your method

在 .Net Core 中,上下文中有一个 ActionArguments IDictionary,包含方法中的所有参数

So if you have the following controller method

所以如果你有以下控制器方法

    [HttpPost]
    public void Post([FromBody]BaseRequest request)
    {
    }

You can access the field like so

您可以像这样访问该字段

    public override void OnActionExecuting(ActionExecutingContext context)
    {
      var request = context.ActionArguments["request"] as BaseRequest;`
      //do whatever, 
    }

回答by Arun Kumar A.J

Making the base.OnActionExecuted()call the last line of the method solved the 'Model being null' problem for me.

使得base.OnActionExecuted()调用该方法的最后一行解决了“模式被空”的问题对我来说。

(This is a comment to @Steven Lyons 's answer, but I'm posting as an answer because I can't comment.)

(这是对@Steven Lyons 的回答的评论,但我将其作为回答发布,因为我无法发表评论。)