在操作签名中选择对象绑定器

时间:2012-07-05 14:56:54

标签: asp.net-mvc-3 action method-signature defaultmodelbinder custom-model-binder

我有一个自定义模型绑定器来从会话中获取数据,但我也想不时使用默认绑定器。

是否可以在操作签名中选择模型绑定器而不是使用UpdateModel?

实施例

public ViewResult MyAction(Cart useSessionBinder, Cart useFormData)
{}

由于

2 个答案:

答案 0 :(得分:1)

您可以使用[ModelBinder]属性:

public ActionResult MyAction(
    [ModelBinder(typeof(MyCustomModelBinder))] Cart useSessionBinder, 
    Cart useFormData
)
{
    ...
}

显然,您应该没有将自定义模型绑定器全局分配到Cart中的Application_Start类,否则它将自动应用于Cart作为操作参数出现的所有实例。< / p>

答案 1 :(得分:0)

好吧,您可以在Appliaction_Start中保留全局自定义模型绑定器,让它在任何地方都可以使用,并在需要时不时重置为默认绑定器

public ActionResult MyAction(
       [ModelBinder(typeof(DefaultModelBinder))]  Cart useFormData)