尝试在编辑表单中保存小数时出错



我的模型中有这个字段:

[Required(ErrorMessage = "Il budget é richiesto")]
[DataType(DataType.Currency)]
[Display(Name = "Budget")]
public decimal ContactBudget { get; set; }

当我在创建页面中保存值1000时,一切都很好。

当我尝试编辑时,文本框中显示的值不是1000而是1000,00

<div class="form-group">
<label asp-for="ContactBudget" class="control-label"></label>
<input type="text" asp-for="ContactBudget" class="form-control" />
<span asp-validation-for="ContactBudget" class="text-danger"></span>
</div>

当我试图保存时,我得到了这个错误:

The field Budget must be a number.

如果我试图在1000.00中更改,我会得到另一个错误:

The value '1000.00' is not valid for Budget.

但如果我重写1000,我可以保存表单。

该怎么办才能允许值1000,00?

感谢

Entity Framework在SQL中将Decimal变量类型转换为Decimal(18,0(。

所以更改属性


[DataType(DataType.Currency)]
[Display(Name = "Budget")]
[Precision(18, 2)]
public decimal ContactBudget { get; set; }

并进行迁移。DatType货币只是添加了一个货币符号。

如果你仍然有逗号而不是点的问题,那么试着用类型号替换类型文本

<input type="number" asp-for="ContactBudget" class="form-control" />

或者你可以尝试使用这个数据注释代替

[Column(TypeName = "decimal(18,2)")]
public decimal ContactBudget { get; set; }

最新更新