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

查看:138
本文介绍了Typeconverter在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未考虑由TypeConverters装饰的 属性 .

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

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

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