类型转换器在 asp.net 核心中不起作用

编程入门 行业动态 更新时间:2024-10-24 20:21:19
本文介绍了类型转换器在 asp 核心中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我将 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 核心.

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 core.

推荐答案

根据我的观察 asp core 没有考虑到 properties 由 TypeConverters 修饰.

Based on my observations asp 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 核心中不起作用

本文发布于:2023-11-16 10:09:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603332.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换器   中不   核心   类型   asp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!