类型转换器在 asp.net 核心中不起作用 [英] Typeconverter does not work in asp.net core

查看:14
本文介绍了类型转换器在 asp.net 核心中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have Amount stored in the database as decimal. I want to show that value on UI with thousand separator. I can add [DisplayFormat(DataFormatString = "{0:N2}", ApplyFormatInEditMode = true)] attribute on amount property and that would display number with thousand separator however when i POST the value back to server, the MVC model binding would not work because of commas.

我创建了一个自定义类型转换器,可以将 decimal 转换为 string,然后将 string 转换为 decimal

I have created a custom type converter that converts from decimal to string and then string to decimal

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;
    }
}

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

and then i applied it on one of the model property. Note that model may have other decimal properties which may not required this conversion.

public class MyModel
{

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

    public string Name { get; set; }
}

Index.cshtml

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

但是,转换器代码永远不会被触发.当我在 NumberConverter 中放置断点时,没有一个断点命中.我需要在任何地方注册类型转换器吗?我正在使用 asp.net 核心.

However the converter code never gets fired. When i put break point in NumberConverter none of the break point hit. Do i need to register type converter anywhere? I am using asp.net core.

推荐答案

根据我的观察 asp.net core 没有考虑到 propertiesTypeConverters 修饰.

Based on my observations asp.net core doesn't take into account properties decorated by TypeConverters.

所以它只支持装饰实际类型类声明TypeConverters.

So it only supports TypeConverters that are decorating actual type class declaration.

作品

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

}

不起作用

public class OtherModel
{
    [TypeConverter(typeof(MyModelStringTypeConverter))]
    public MyModel MyModel { get;set; }
}

这篇关于类型转换器在 asp.net 核心中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆