Nancy: ВозмоТная ΠΏΡ€ΠΎΠ±Π»Π΅ΠΌΠ° привязки ΠΌΠΎΠ΄Π΅Π»ΠΈ с дСсятичными Π·Π½Π°ΠΊΠ°ΠΌΠΈ

Π‘ΠΎΠ·Π΄Π°Π½Π½Ρ‹ΠΉ Π½Π° 1 июл. 2014  Β·  3ΠšΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ  Β·  Π˜ΡΡ‚ΠΎΡ‡Π½ΠΈΠΊ: NancyFx/Nancy

http://stackoverflow.com/questions/24439997/nancy-decimal-property-binding-not-working

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)

ΠŸΠΎΡ…ΠΎΠΆΠ΅, это ошибка, сам ΠΏΠΎΠΏΡ€ΠΎΠ±ΠΎΠ²Π°Π» с ΠΌΠ°Ρ€ΡˆΡ€ΡƒΡ‚ΠΎΠΌ ΠΈ дСсятичной Π΄Ρ€ΠΎΠ±ΡŒΡŽ ΠΈ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΠ» Ρ‚Π°ΠΊΠΎΠ΅ ΠΆΠ΅ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅.

Π‘Π°ΠΌΡ‹ΠΉ ΠΏΠΎΠ»Π΅Π·Π½Ρ‹ΠΉ ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ

ΠŸΡ€ΠΎΡΡ‚ΠΎ Π½Π°ΠΏΠΈΡˆΠΈΡ‚Π΅ собствСнный ITypeConverter

`` Π‘ #
///


/// ΠšΠΎΠ½Π²Π΅Ρ€Ρ‚Π΅Ρ€ Nancy для прСобразования числовых Ρ‚ΠΈΠΏΠΎΠ² с ΠΏΠΎΠΌΠΎΡ‰ΡŒΡŽ InvariantCulture.
///

ΠΎΡ‚ΠΊΡ€Ρ‹Ρ‚Ρ‹ΠΉ класс NancyNumericConverter: ITypeConverter
{
public bool CanConvertTo (Ρ‚ΠΈΠΏ destinationType, контСкст BindingContext)
{
Π²Π΅Ρ€Π½ΡƒΡ‚ΡŒ 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);
    }
}

`` ''

ВсС 3 ΠšΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ

DecimalConverter.ConvertFrom ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅Ρ‚ΡΡ Π² контСкстС Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΉ ΠΊΡƒΠ»ΡŒΡ‚ΡƒΡ€Ρ‹, ΠΎΠ½ ΠΎΠΆΠΈΠ΄Π°Π΅Ρ‚ Β«1,1Β» вмСсто Β«1,1Β» Π² Ρ‚ΡƒΡ€Π΅Ρ†ΠΊΠΎΠΉ ΠΊΡƒΠ»ΡŒΡ‚ΡƒΡ€Π΅. РСзСрвная стратСгия ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ Ρ€Π΅Π°Π»ΠΈΠ·ΠΎΠ²Π°Π½Π° с ΠΏΠΎΠΌΠΎΡ‰ΡŒΡŽ ConvertFrom с InvariantCulture. Π’Ρ‹ ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π΅Ρ‚Π΅ запросы Π½Π° вытягиваниС?

ΠŸΡ€ΠΎΡΡ‚ΠΎ Π½Π°ΠΏΠΈΡˆΠΈΡ‚Π΅ собствСнный ITypeConverter

`` Π‘ #
///


/// ΠšΠΎΠ½Π²Π΅Ρ€Ρ‚Π΅Ρ€ Nancy для прСобразования числовых Ρ‚ΠΈΠΏΠΎΠ² с ΠΏΠΎΠΌΠΎΡ‰ΡŒΡŽ InvariantCulture.
///

ΠΎΡ‚ΠΊΡ€Ρ‹Ρ‚Ρ‹ΠΉ класс NancyNumericConverter: ITypeConverter
{
public bool CanConvertTo (Ρ‚ΠΈΠΏ destinationType, контСкст BindingContext)
{
Π²Π΅Ρ€Π½ΡƒΡ‚ΡŒ 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);
    }
}

`` ''

ΠŸΡ€ΠΎΡΡ‚ΠΎ Π½Π°ΠΏΠΈΡˆΠΈΡ‚Π΅ собствСнный ITypeConverter

Бпасибо Π·Π° ΠΏΡ€Π΅Π΄Π»ΠΎΠΆΠ΅Π½ΠΈΠ΅, ΠΎΡ‚Π»ΠΈΡ‡Π½ΠΎ Ρ€Π°Π±ΠΎΡ‚Π°Π΅Ρ‚!
Но ΠΏΠΎΡ…ΠΎΠΆΠ΅, Ρ‡Ρ‚ΠΎ System.Convert.ChangeType Π½Π΅ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Π΅Ρ‚ Ρ‚ΠΈΠΏΡ‹, Π΄ΠΎΠΏΡƒΡΠΊΠ°ΡŽΡ‰ΠΈΠ΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ NULL.

МоТно ΠΈΡΠΏΡ€Π°Π²ΠΈΡ‚ΡŒ с ΠΏΠΎΠΌΠΎΡ‰ΡŒΡŽ Nullable.GetUnderlyingType Π²ΠΎΡ‚ Ρ‚Π°ΠΊ:

/// <summary>
/// Nancy converter to convert numeric types with InvariantCulture.
/// </summary>
public class InvariantCultureNumericConverter : 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;
        }

        destinationType = Nullable.GetUnderlyingType(destinationType) ?? destinationType;

        return System.Convert.ChangeType(input, destinationType, CultureInfo.InvariantCulture);
    }
}
Π‘Ρ‹Π»Π° Π»ΠΈ эта страница ΠΏΠΎΠ»Π΅Π·Π½ΠΎΠΉ?
0 / 5 - 0 Ρ€Π΅ΠΉΡ‚ΠΈΠ½Π³ΠΈ