tags:

views:

34

answers:

1

The thing is that all my ids are encrypted and I have 2 methods that parses the Id from long to the Encrypted (something like A8sdf=dsfs=) and back so I have to do this conversion in each action method when I send or receive an EncryptedId

Is it possible to modify the value for a specific type or Property Name before it reaches the action method ?

+1  A: 

You could write a model binder that will decrypt the value:

public class MyModelBinder : DefaultModelBinder
{
    protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, object value)
    {
        // Test if it is the Id property
        if (propertyDescriptor.Name == "Id")
        {
            // Remark: MyDecryptFunction must return the same type
            value = MyDecryptFunction(value);
        }
        base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
    }

}
Darin Dimitrov
could you show me which method should I override and how change the value of a specific type or property using the modelbinder ?
Omu
The `SetProperty` method seems appropriate here.
Darin Dimitrov
can I use the ModelBinder to modify the value that goes to the view ?
Omu