Asp.NET MVC模型绑定器,让操作方法 [英] Asp.NET MVC ModelBinder, getting Action Method

查看:186
本文介绍了Asp.NET MVC模型绑定器,让操作方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的模型绑定器,我想获得的动作。因为我想获得使用反射动作的属性,动作的名称是远远不够的。

I got a custom ModelBinder and i would like to get the action. Because i want to get the Attributes of the action using reflection, the action name is not enough.

我的操作方法:

[MyAttribute]
public ActionResult Index([ModelBinder(typeof(MyModelBinder))] MyModel model)
{
}

和这里一个典型的ModelBinder的

and here a typically ModelBinder

public class MyModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    {
        // here i would like to get the action method and his "MyAttribute"
    }
}

任何建议,其他的解决方案?

any suggestions, other solutions ?

提前很多感谢

推荐答案

没有,你不能100%的把握获得一个模型绑定当前操作。该模型粘合剂不耦合到的动作,但是要结合到模型。例如,你可以叫

No, you cannot with 100% certainty get the current action from a model binder. The model binder is not coupled to the action, but to binding to a model. For example, you can call

TryUpdateMode(model)

在一个过滤器的动作已被选定之前。另外请注意,操作方法甚至可能不是一个CLR方法(见的 http://haacked.com/archive/2009/02/17/aspnetmvc-ironruby-with-filters.aspx ),可以反映。

In an filter before an action has been chosen. Also note that an action method might not even be a CLR method (see http://haacked.com/archive/2009/02/17/aspnetmvc-ironruby-with-filters.aspx) that can be reflected on.

我觉得真正的问题是,究竟是你想什么来完成,这是正确的做法?如果你想从动作的信息要传递给模型绑定(听取意见,你的模型绑定应该适度降低,如果信息不存在),你应该使用一个动作过滤器放在HttpContext.Items中的信息(或地方像),然后让粘结剂检索。

I think the real question is, what exactly are you trying to accomplish and is this the right way? If you want information from the action to be passed to the model binder (heeding the advice that your model binder should degrade gracefully if the information isn't there), you should use an action filter to put the information in HttpContext.Items (or somewhere like that) and then have your binder retrieve it.

这是动作过滤器的OnActionExecuting方法接收它有一个ActionDescriptor的ActionExecutingContext。你可以调用该GetCustomAttributes。

An action filter's OnActionExecuting method receives an ActionExecutingContext which has an ActionDescriptor. You can call GetCustomAttributes on that.

这篇关于Asp.NET MVC模型绑定器,让操作方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆