문제

I have a custom model binder to get data from session, but I also would like to use the default binder from time to time.

Is it possible to choose model binder in action signature instead of using UpdateModel?

Example

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

Thanks

도움이 되었습니까?

해결책

You could use the [ModelBinder] attribute:

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

Obviously you should have not assigned globally your custom model binder to the Cart class in your Application_Start, otherwise it will automatically apply to all instances of Cart appearing as action parameters.

다른 팁

Well, you can keep global custom model binder in Appliaction_Start, to let it work everywhere, and reset back to default binder from time to time, when needed

public ActionResult MyAction(
       [ModelBinder(typeof(DefaultModelBinder))]  Cart useFormData)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top