lundi 29 juin 2015

Model binder in Invariant mode

How to configure mvc model binder to make it work in invariant mode? that's the problem

The only solution that managed to find - is to write your own model binder. But is it possible to make a standard work in the invariant mode?

EDIT An example of a custom binder for decimal numbers.

public class DecimalModelBinder : IModelBinder {
public object BindModel(ControllerContext controllerContext, 
    ModelBindingContext bindingContext) {
    ValueProviderResult valueResult = bindingContext.ValueProvider
        .GetValue(bindingContext.ModelName);
    ModelState modelState = new ModelState { Value = valueResult };
    object actualValue = null;
    try {
        actualValue = Convert.ToDecimal(valueResult.AttemptedValue, 
            System.Globalization.CultureInfo.InvariantCulture);
    }
    catch (FormatException e) {
        modelState.Errors.Add(e);
    }

    bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
    return actualValue;
}

}

Is it possible to do without it ? Set up the standard mvc binder to work with decimal numbers in the invariant mode.

Aucun commentaire:

Enregistrer un commentaire