Typeconverter无法在asp.net核心中运行

LP1*_*P13 9 asp.net-core-mvc coreclr asp.net-core asp.net-core-1.0

我已经Amount存储在数据库中了decimal.我希望用千位分隔符在UI上显示该值.我可以[DisplayFormat(DataFormatString = "{0:N2}", ApplyFormatInEditMode = true)]在amount属性上添加属性,这将显示千位分隔符的数字,但是当我将值POST回服务器时,由于逗号,MVC模型绑定将不起作用.

我创建了一个自定义类型转换器,可以转换decimalstring然后转换stringdecimal

public class NumberConverter : TypeConverter
{
    public override bool CanConvertFrom(
        ITypeDescriptorContext context,
        Type sourceType)
    {
        if (sourceType == typeof(decimal))
        {
            return true;
        }
        return base.CanConvertFrom(context, sourceType);
    }
    public override object ConvertFrom(ITypeDescriptorContext context,
        CultureInfo culture, object value)
    {
        if (value is decimal)
        {
            return string.Format("{0:N2}", value);
        }
        return base.ConvertFrom(context, culture, value);
    }
    public override bool CanConvertTo(ITypeDescriptorContext context,
        Type destinationType)
    {
        if (destinationType == typeof(decimal))
        {
            return true;
        }
        return base.CanConvertTo(context, destinationType);
    }
    public override object ConvertTo(ITypeDescriptorContext context,
        CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(decimal) && value is string)
        {
            return Scrub(value.ToString());
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }

    private decimal Scrub(string modelValue)
    {
        NumberStyles _currencyStyle = NumberStyles.Currency;
        CultureInfo _culture = new CultureInfo("en-US");

        var modelDecimal = 0M;
        decimal.TryParse(
           modelValue,
           _currencyStyle,
           _culture,
           out modelDecimal
       );

        return modelDecimal;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我将其应用于其中一个模型属性.请注意,模型可能具有其他十进制属性,可能不需要此转换.

public class MyModel
{

    [TypeConverter(typeof(NumberConverter))]
    [Display(Name = "Enter Amount")]
    public decimal Amount { get; set;}

    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

Index.cshtml

<form asp-action="submit" asp-controller="home">
    @Html.EditorFor(m => m.Amount)
    <button type="submit" class="btn btn-primary">Save</button>
</form>
Run Code Online (Sandbox Code Playgroud)

但是转换器代码永远不会被触发.当我把破发点放在NumberConverter没有破点的时候.我需要在任何地方注册类型转换器吗?我使用的是asp.net核心.

Ara*_*yan 6

根据我的观察asp.net core并没有考虑到性能的装饰TypeConverters

所以它只支持TypeConverters修饰实际类型的类声明

作品

[TypeConverter(typeof(MyModelStringTypeConverter))]
public class MyModel
{

}
Run Code Online (Sandbox Code Playgroud)

不起作用

public class OtherModel
{
    [TypeConverter(typeof(MyModelStringTypeConverter))]
    public MyModel MyModel { get;set; }
}
Run Code Online (Sandbox Code Playgroud)

  • 我遇到了同样的问题 - 在模型内部我有我想从字符串解析的值的集合/集 - 到目前为止,我想出了编写简单的转换器并以这种方式从`Startup`“注册”它:`TypeDescriptor.AddAttributes( typeof (IReadOnlyCollection&lt;string&gt;),new TypeConverterAttribute( typeof(FooTypeConverter)));` ...但我不太喜欢这种方法:( (2认同)