Nancy: مشكلة محتملة في ربط النموذج بالأرقام العشرية

تم إنشاؤها على ١ يوليو ٢٠١٤  ·  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

ج #
///


/// محول نانسي لتحويل الأنواع الرقمية مع InvariantCulture.
///

فئة عامة NancyNumericConverter: ITypeConverter
{
منطقية عامة CanConvertTo (نوع وجهة النوع ، سياق BindingContext)
{
الوجهة المرتجعة Type.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

ج #
///


/// محول نانسي لتحويل الأنواع الرقمية مع InvariantCulture.
///

فئة عامة NancyNumericConverter: ITypeConverter
{
منطقية عامة CanConvertTo (نوع وجهة النوع ، سياق BindingContext)
{
الوجهة المرتجعة Type.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 لا يدعم الأنواع الفارغة.

يمكن إصلاحه باستخدام 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 التقييمات