Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nancy decimal property binding not working

Tags:

c#

nancy

I'm having problem with binding decimal property:

Oh noes! ---> Nancy.ModelBinding.ModelBindingException: Unable to bind to type: Nancy.Models.SomeModel
at Nancy.ModelBinding.DefaultBinder.Bind(NancyContext context, Type modelType, Object instance, BindingConfig configuration, String[] blackList)
at Nancy.ModelBinding.DynamicModelBinderAdapter.TryConvert(ConvertBinder binder, Object& result)
at CallSite.Target(Closure , CallSite , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
at Nancy.ModelBinding.ModuleExtensions.Bind[TModel](INancyModule module)
at KBZServisNancy.Modules.SomeModule.<.ctor>b__2(Object x) in d:\Nancy\Modules\SomeDecimalModule.cs:line 38
at CallSite.Target(Closure , CallSite , Func`2 , Object )
at Nancy.Routing.Route.<>c__DisplayClass4.b__3(Object parameters, CancellationToken context)
--- End of inner exception stack trace ---
at Nancy.NancyEngine.InvokeOnErrorHook(NancyContext context, ErrorPipeline pipeline, Exception ex)
at Nancy.NancyEngine.InvokeOnErrorHook(NancyContext context, ErrorPipeline pipeline, Exception ex)

If value of decimal property is for example 20,50 binding works fine, but if value is 20.50 I get upper error message. According to this, there may be problem with culture sensitive but context culture is {en-US}. There were already some issues of this kind Culture model binding and they are solved so I dont't have idea where the problem could be. I'm using Nancy v0.23 with self hosting in console application. Thanks for your help in advance.

like image 211
rmares Avatar asked Mar 20 '26 05:03

rmares


2 Answers

In the current Nancy version just write own ITypeConverter

/// <summary>
/// Nancy converter to convert numeric types with InvariantCulture.
/// </summary>
public class NancyNumericConverter : ITypeConverter
{
    public bool CanConvertTo(Type destinationType, BindingContext context)
    {
        return destinationType.IsNumeric();
    }

    public object Convert(string input, Type destinationType, BindingContext context)
    {
        if (string.IsNullOrEmpty(input))
        {
            return null;
        }

        return System.Convert.ChangeType(input, destinationType, CultureInfo.InvariantCulture);
    }
}

(I have updated the issue https://github.com/NancyFx/Nancy/issues/1587 too.)

like image 176
xmedeko Avatar answered Mar 21 '26 18:03

xmedeko


I've raised this as an issue, looks like a culture issue, works fine when its US, but if I change my local culture to Turkey and try with a decimal point I get the exception also.

https://github.com/NancyFx/Nancy/issues/1587

like image 44
Phill Avatar answered Mar 21 '26 18:03

Phill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!